多文件上传
多文件上传和单文件上传差不多,只是有个数组接收而已:
页面:
<body> <form action="${pageContext.request.contextPath }/file/upload.action"method="post" enctype="multipart/form-data"> 请选择文件:<input type="file" name="files"/><br/> 请选择文件:<input type="file" name="files"/><br/> 请选择文件:<input type="file" name="files"/><br/> <input type="submit" value="上传" /> </form> </body>
配置文件中(和单文件上传是一样的):
<constant name="struts.multipart.saveDir" value="d://"/> <package name="file" namespace="/file"extends="struts-default"> <action name="upload" class="hwt.action.FileUploadAction"> <interceptor-ref name="fileUpload"> <param name="maximumSize">1024</param> <param name="allowedTypes"> text/plain,application/msword <!--image/png,image/gif,image/jpeg --> </param> </interceptor-ref> <!-- 要放在后面,要不然不会起到作用 --> <interceptor-ref name="defaultStack"/> <!-- 还需要配置input为错误输出页面 --> <result name="input">/fileUpload.jsp</result> <result name="success">/downLoad.jsp</result> <result name="failure">/fileUpload.jsp</result> </action> </package>
Action中
要继承ActionSupport
/
* * ActionSupport里面提供了validate的校验功能 * 实际上继承ActionSupport之后 * 就等同于实现了很多接口Action,Validateable,ValidationAWare,TextProvider,LoacalProvider,Serializable * 可以提供数据校验序列化国际化等功能 */ public class FileUploadAction extends ActionSupport{ private File[] files ; //这个名字是struts2会自动填充,但是取名必须是-属性名ContentType; private String[] filesContentType; //这个名字是struts2会自动填充,但是取名必须是-属性名FileName; private String[] filesFileName; public String execute() throws IOException{ if (files.length!=0 && files != null) { int len = files.length; String realPath = ServletActionContext.getServletContext().getRealPath(FileUploadUtils.baseName); String savepath = FileUploadUtils.getSavePath(realPath); for(int i = 0 ; i < len ; i++){ String uuidFileName = FileUploadUtils.getUUIDName(filesFileName[i]); File descFile = new File(savepath,uuidFileName); //这个工具类是commons.io包下面的 FileUtils.copyFile(files[i], descFile); } return "success"; }else { return "failure"; } } public File[] getFiles() { return files; } public void setFiles(File[] files) { this.files = files; } public String[] getFilesContentType() { return filesContentType; } public void setFilesContentType(String[] filesContentType) { this.filesContentType = filesContentType; } public String[] getFilesFileName() { return filesFileName; } public void setFilesFileName(String[] filesFileName) { this.filesFileName = filesFileName; } //setter getter }
工具类中:
public class FileUploadUtils { public static String baseName; //单利模式得到属性文件中的文件上传的配置 static{ Properties fileProperties = new Properties(); InputStream iStream =FileUploadUtils.class.getResourceAsStream("/fileUpload.properties"); try { fileProperties.load(iStream); } catch (IOException e) { // TODO Auto-generatedcatch block e.printStackTrace(); } baseName = fileProperties.getProperty("basepath"); } //把上传的文件名字改成UUID public static String getUUIDName(String fileName){ UUID uuid = UUID.randomUUID(); String extension = fileName.substring(fileName.lastIndexOf(".")); return uuid+extension; } //得到服务器上的保存路径 public static String getSavePath(String realPath){ //对上传的文件进行分级 Calendar calendar = new GregorianCalendar(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day =calendar.get(Calendar.DAY_OF_MONTH); String savePath = realPath+"\\"+year+"\\"+month+"\\"+day; File file = new File(savePath); if (!file.exists()) { file.mkdirs(); } System.out.println(savePath); return savePath; } }