您的位置:首页 > Web前端 > HTML

文件上传 commons-FileUpload

2009-11-24 14:40 169 查看
 

actionForm:

/**
*
* @version 1.0
* @author shen longguang
* 2009-11-24
*/
public class FileUploadForm extends ValidatorForm {
private static final long serialVersionUID = 1L;
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return super.validate(mapping, request);
}
} 


 

action:(BaseAction继承ActionSupport)

/**
* 文件上传
* @version 1.0
* @author shen longguang
* 2009-11-24
*/
public class FileUploadAction extends BaseAction {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String path;// 得到该应用下的upload目录在服务器上的绝对路径
path = request.getSession().getServletContext().getRealPath("/upload");
ActionMessages errors = new ActionErrors();
if (path == null) {// 如果为空表示不能访问,可能是系统设置了访问权限
errors.add("pathError", new ActionMessage("fileUpload.pathError"));
return mapping.findForward("faile");
}

File uploadDir = new File(path);// 得到该目录的文件对象
if (!uploadDir.exists()) {// 判断该目录是否存在
if (!uploadDir.mkdir()) {// 如果不存在就建立该目录
/* 如果建立失败,给出提示 */
errors.add("mkdirError", new ActionMessage("fileUpload.mkdirError"));
return mapping.findForward("faile");
}
}

if(!ServletFileUpload.isMultipartContent(request)){//用 ServletFileUpload 类的静态方法 isMultipartContent 判断 request 是否是 multipart/form-data 类型
/* 只能 multipart/form-data 类型数据 */
errors.add("fileError", new ActionMessage("fileUpload.fileError"));
return mapping.findForward("faile");
}
//创建 DiskFileItemFactory 对象
DiskFileItemFactory factory = new DiskFileItemFactory();
//		File repsFile = new File(path+"/temp");
factory.setRepository(new File("c:/TIMS_FILEUPLOAD/TEMP"));//可以设置缓存目录
factory.setSizeThreshold(1024 * 1024*2);//超过1m的数据采用临时文件缓存

//创建 ServletFileUpload 对象,构造的时候传一个 DiskFileItemFactory 对象进去
ServletFileUpload upload = new ServletFileUpload(factory);

upload.setFileSizeMax(1024 * 1024 * 50);//单个文件大小50M
upload.setHeaderEncoding("utf-8");//设置普通字段名称和文件字段的文件名所采用的字符集编码
upload.setSizeMax(1024 * 1024 * 100);//最多上传100m的文件

FileUploadForm fuForm = (FileUploadForm)form;//得到表单控件
FormFile file = fuForm.getFile();
String fileName = file.getFileName();
FileOutputStream out = new FileOutputStream(path + File.separator + fileName);
out.write(file.getFileData());
this.saveErrors(request, errors);
return mapping.findForward("success");
}
}


 

 

显示层 jsp:

<%@ page language="java"  pageEncoding="UTF-8"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>文件上传</head>

<body>
<html:form action="fileupload" method="post" enctype="multipart/form-data">
file :	<html:file property="file"/>
<br>
<html:submit/><html:cancel/>
</html:form>

<a href="test.do?p=saveIn" mce_href="test.do?p=saveIn"">Medicine add test</a>
</body>
</html>


 

struts-config.xml

<form-bean name="fileUploadForm"
type="xx.xxx.struts.form.FileUploadForm" />


 
<action path="/fileupload"
name="fileUploadForm"
type="xxx.xxx.FileUploadAction"
validate="false">
<forward name="success"
path="/success.jsp" />
<forward name="faile"
path="/faile.jsp" />
</action>


参考文档: http://commons.apache.org/fileupload/apidocs/index.html
 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息