struts2 实现多文件限制上传
struts2的文件上传特别简单,把我做的小例子给大家看一下
upload.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
< %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> < %@ taglib prefix="s" uri="/struts-tags" %> < % String path = request.getContextPath(); pageContext.setAttribute("context",path); %> < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> </meta><meta http-equiv="cache-control" content="no-cache"> </meta><meta http-equiv="expires" content="0"> </meta><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> </meta><meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function more() { var td = document.getElementById("more"); var br = document.createElement("br"); var input = document.createElement("input"); input.type="file"; input.name="uf.upload"; var button = document.createElement("input"); button.type="button"; button.value="remove"; td.appendChild(br); td.appendChild(input); td.appendChild(button); button.onclick = function() { td.removeChild(br); td.removeChild(input); td.removeChild(button); } } </script> </meta></head> <body> <h1> struts2上传不定数的文件,限制文件上传类型(struts.xml配置拦截器) </h1> <hr /> <s :fielderror></s> <table align="center" border="1" width="50%"> <s :form action="upload" enctype="multipart/form-data" method="post" theme="simple"> <tr> <td> 用户名: </td> <td> <s :textfield name="uf.username"></s> </td> </tr> <tr> <td> 上传: </td> <td id="more"> <s :file name="uf.upload"></s><font color="red">*只能上传.jpg文件,小于400kb</font> <input type="button" value="添加.." onclick="more()"/> </td> </tr> <tr> <td align="center"> <s :submit value="提交"></s> </td> <td><s :reset value="重置"/></td> </tr> </s> </table> </body> </html> |
我把jsp上传的内容封装成了一个类UploadFile.java,下面代码显示有点问题,泛型里的好像都出错了应该是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package pojo; import java.io.File; import java.util.List; public class UploadFile { private String username; private List<file> upload; private List<string> uploadFileName; private List</string><string> uploadContentType; public List<file> getUpload() { return upload; } public void setUpload(List</file><file> upload) { this.upload = upload; } public List<string> getUploadContentType() { return uploadContentType; } public void setUploadContentType(List</string><string> uploadContentType) { this.uploadContentType = uploadContentType; } public List</string><string> getUploadFileName() { return uploadFileName; } public void setUploadFileName(List</string><string> uploadFileName) { this.uploadFileName = uploadFileName; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } } </string></file></string></file> |
接着是struts.xml的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
< ?xml version="1.0" encoding="UTF-8" ?> < !DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 更改字符集编码 在org.apache.struts2.default.properties 定义--> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.multipart.maxSize" value="40960000"></constant> <!-- 显示错误信息时,src下有message.properties,要在这里配置,才显示自己写的错误信息--> <constant name="struts.custom.i18n.resources" value="message"></constant> <action name="upload" class="action.UploadAction"> <result name="success">/uploads/result.jsp</result> <result name="input">/uploads/upload.jsp</result> <interceptor -ref name="fileUpload"> <!-- 在org.apache.struts2.interceptor.FileUploadInterceptor找限制大小和类型的参数 限制每个每个上传文件的大小,允许上传的类型,类型在tomcat-->conf-->web.xml中寻找 --> <param name="maximumSize">40960000</param> <param name="allowedTypes">image/jpeg,image/jpeg</param> </interceptor> <!-- 默认拦截器defaultStack要显示的调用,当你调用其他的拦截器后,默认拦截器不会再自动调用 --> <interceptor -ref name="defaultStack"></interceptor> </action> </struts> |
接着看UploadAction.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import java.util.List; import org.apache.struts2.ServletActionContext; import sjzpc.pojo.UploadFile; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private UploadFile uf; public UploadFile getUf() { return uf; } public void setUf(UploadFile uf) { this.uf = uf; } @Override public String execute() throws Exception { for(int i = 0; i < uf.getUpload().size(); i++) {//多文件上传 InputStream is = new FileInputStream(uf.getUpload().get(i)); // String root = ServletActionContext.getRequest.getRealPath("/uploads"); 这个request.getRealPath()方法已过时,不建议使用 String root = ServletActionContext.getServletContext().getRealPath("/uploads"); OutputStream os = new FileOutputStream(new File(root,uf.getUploadFileName().get(i))); int len = 0; byte[] buffer = new byte[1024];//缓冲,提高效率 while((len = is.read(buffer)) > 0) { os.write(buffer, 0, len); } // 输入输出流记着一定要关闭 os.close(); is.close(); } return "success"; } } |