文件上传 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
相关文章推荐
- 使用commons-fileupload实现文件上传
- commons-fileupload-1.2.2实现文件上传
- commons-fileupload-1.2.1实现文件上传
- servlet+uploadify+commons-fileupload实现文件上传
- java用Commons-fileupload 文件的上传
- 用commons-fileupload-1.2 实现文件上传
- 在上传文件中用commons-fileupload
- commons—fileUpload实现多个文件的上传
- commons-fileupload和dwr实现带有进度条的文件上传
- 利用commons-fileupload组件上传文件至服务器和数据库
- 使用commons-fileupload实现文件上传
- Java中关于文件上传的基本处理方法Commons-FileUpload
- commons-fileupload文件上传、下载
- 用Commons-FileUpload组件实现文件上传
- commons-fileupload实现文件上传功能实例
- 利用commons-fileupload实现文件上传
- commons-fileupload实现文件上传、下载、在线打开
- 在JSP环境中利用Commons-fileupload组件实现文件上传
- JAVAWEB开发之文件的上传与下载(开源组件commons-fileupload的详细使用)
- commons-fileupload 上传文件乱码问题