Struts2使用开源项目Apache Jakarta Commons FileUpload和内建的FileUploadInterceptor拦截器实现文件上传,所需的jar包如下:
commons-logging-1.1.jar freemarker-2.3.8.jar ognl-2.6.11.jar struts2-core-2.0.6.jar xwork-2.0.1.jar commons-io-1.3.1.jar commons-fileupload-1.2.jar
一:单文件止传
1.文件上传页面 fileUpload.jsp
<body> <s:form action="/fileUpload.action" method="post" enctype="multipart/form-data"> <s:fielderror></s:fielderror> <s:file name="myFile" label="图片上传"></s:file> <s:textfield name="caption" label="标题"></s:textfield> <s:submit value="提交"></s:submit> </s:form> </body>
2.创建FileUploadAction.java:处理文件上传
public class FileUploadAction extends ActionSupport { private static final int BUFFER_SIZE = 10*1024; private File myFile; //对应页面文件name private String contentType; //文件类型 private String fileName; //文件名 private String imageFileName; // private String caption; //标题 public void setMyFileContentType(String contentType){ this.contentType = contentType; } public void setMyFileFileName(String fileName){ this.fileName = fileName; } public void setMyFile(File myFile){ this.myFile = myFile; } public String getImageFileName(){ return imageFileName; } public String getCaption(){ return caption; } public void setCaption(){ this.caption = caption; } public static void copy(File src,File dst){ InputStream in = null; //输入流 OutputStream out = null;//输出流 try { in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; while (in.read(buffer)>0 ) { out.write(buffer); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(in!=null){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(out!=null){ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static String getExtention(String fileName){ int pos = fileName.lastIndexOf("."); return fileName.substring(pos); }
@Override public String execute() throws Exception { //创建新的文件名(系统时间+原文件扩展名) Date date = new Date(); String newFileName = this.getExtention(fileName); imageFileName = date.getTime()+newFileName; //创建新文件保存路径 File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages")+"/"+imageFileName); this.copy(myFile, imageFile); return SUCCESS; } }
在fileupload.jsp中,只有doc一个字段,而FileUploadAction.java中,却有三个字段,Struts2怎么通过页面的 一个字段设置Action里的三个字段呢?没错,这就是FileUploadInterceptor的功劳了!你所要做的只是按照一定的样式命名这三个字 段的set方法,而字段名可以任意命名。第一个File类型的字段的set方法还是以常规的方式命名,另两个String类型的字段的set方法必须分别 以“File字段的set方法名+FileName”和“File字段的set方法名+ContentType”来命名。
3.struts.xml配置
<!-- 文件上传配置 --> <package name="fileupload" namespace="/" extends="struts-default"> <action name="fileUpload" class="fileupload.FileUploadAction"> <interceptor-ref name="fileUpload"> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg </param> </interceptor-ref> <interceptor-ref name="fileUploadStack"></interceptor-ref> <result name="input">/fileupload/fileUpload.jsp</result> <result name="success">/fileupload/showUpload.jsp</result> </action> </package>
4.web.xml配置
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5.显示页面:showUpload.jsp
<body> <img src='UploadImages/<s:property value="imageFileName"/>'/> <br> <s:property value="caption"/> </body>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
二:多文件上传
Struts 2实现多文件上传也很简单。你可以将多个<s:file />绑定Action的数组或列表。如下例所示。
< s:form action ="doMultipleUploadUsingList" method ="POST" enctype ="multipart/form-data" > < s:file label ="File (1)" name ="upload" /> < s:file label ="File (2)" name ="upload" /> < s:file label ="FIle (3)" name ="upload" /> < s:submit /> </ s:form >
如果你希望绑定到数组,Action的代码应类似:
private File[] uploads; private String[] uploadFileNames; private String[] uploadContentTypes;
public File[] getUpload() { return this .uploads; } public void setUpload(File[] upload) { this .uploads = upload; }
public String[] getUploadFileName() { return this .uploadFileNames; } public void setUploadFileName(String[] uploadFileName) { this .uploadFileNames = uploadFileName; }
public String[] getUploadContentType() { return this .uploadContentTypes; } public void setUploadContentType(String[] uploadContentType) { this .uploadContentTypes = uploadContentType; }
如果你想绑定到列表,则应类似:
private List < File > uploads = new ArrayList < File > (); private List < String > uploadFileNames = new ArrayList < String > (); private List < String > uploadContentTypes = new ArrayList < String > ();
public List < File > getUpload() { return this .uploads; } public void setUpload(List < File > uploads) { this .uploads = uploads; }
public List < String > getUploadFileName() { return this .uploadFileNames; } public void setUploadFileName(List < String > uploadFileNames) { this .uploadFileNames = uploadFileNames; }
public List < String > getUploadContentType() { return this .uploadContentTypes; } public void setUploadContentType(List < String > contentTypes) { this .uploadContentTypes = contentTypes; }
|