一、依赖
Struts2利用第三方文件上传框架:(Myeclispse已配置)进行封装,没有jar需自己加上。
Library URL Struts 2.0.x Struts 2.1.x
Commons-FileUpload http://commons.apache.org/fileupload/ 1.1.1 1.2.1
Commons-IO http://commons.apache.org/io/ 1.0 1.3.2
二、单个文件上传
JSP:
<s:form action="doUpload" method="post" enctype="multipart/form-data"> <s:file name="doc" label="File"/> <s:submit/>
注:那个file 的name是自己取的。
action:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport { private File doc; private String docContentType; //文件类型属性 private String docFileName; //文件名 private String path; public String execute() { FileOutputStream fos = null; FileInputStream fis = null; try { // 建立文件输出流 String filename = getFileName(getDocFileName()); fos = new FileOutputStream(getPath() + "\" + filename); // 建立文件上传流 fis = new FileInputStream(getDoc()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } catch (Exception e) { System.out.println("文件上传失败"); e.printStackTrace(); } finally { close(fos, fis); } return SUCCESS; } private void close(FileOutputStream fos, FileInputStream fis) { if (fis != null) { try { fis.close(); } catch (IOException e) { System.out.println("FileInputStream关闭失败"); e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { System.out.println("FileOutputStream关闭失败"); e.printStackTrace(); } } } //生成一个新的文件名 private String getFileName(String fileName) { int position = fileName.lastIndexOf("."); String extension = fileName.substring(position); return System.currentTimeMillis()+extension; } public String getDocFileName() { return docFileName; } public void setDocFileName(String docFileName) { this.docFileName = docFileName; } public String getPath() { return ServletActionContext.getServletContext().getRealPath(path); } public void setPath(String path) { this.path = path; } public String getDocContentType() { return docContentType; } public void setDocContentType(String docContentType) { this.docContentType = docContentType; } public File getDoc() { return doc; } public void setDoc(File doc) { this.doc = doc; } }
action配置(用了个参数,我觉得不错)
<action name="doUpload" class="com.meetcomet.util.FileUploadAction"> <param name="path">/upload</param> <result name="input">/index.jsp</result> <result name="success">/upload_success.jsp</result> </action>
调试过程中的错误:
1)上传文件夹不存在。
2)action中的属性缺乏getset。(直接用是会错的...值没有赋上去。)
三、实现文件上传过滤
通过配置拦截器来实现的
<struts> <constant name="struts.devMode" value="true" /> <constant name="struts.custom.i18n.resources" value="globalMessg" /> <constant name="struts.il8n.encoding" value="utf-8"/> <!-- 指定允许上传的文件最大字节数。默认值是2097152(2M) --> <constant name="struts.multipart.maxSize" value="10701096"/> <!-- 设置上传文件的临时文件夹,默认使用javax.servlet.context.tempdir --> <constant name="struts.multipart.saveDir " value="d:/tmp" /> <package name="/" extends="struts-default"> <action name="doUpload" class="com.meetcomet.util.FileUploadAction"> <param name="path">/upload</param> <result name="input">/index.jsp</result> <result name="success">/upload_success.jsp</result> <interceptor-ref name="fileUpload"> <!-- 文件过滤 --> <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/pjpeg</param> <!-- 文件大小, 以字节为单位 --> <param name="maximumSize">1025956</param> </interceptor-ref> <!-- 默认拦截器必须放在fileUpload之后,否则无效 --> <interceptor-ref name="defaultStack" /> </action> </package></struts>