struts2學習(14)struts2文件上傳和下載(4)多個文件上傳和下載
阿新 • • 發佈:2017-07-02
sym ring spl out urn ide http iso length
四、多個文件上傳:
五、struts2文件下載:
多個文件上傳action
com.cy.action.FilesUploadAction.java:
package com.cy.action; import java.io.File; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionSupport; public class FilesUploadAction extends ActionSupport{ private static final long serialVersionUID = 1L;View Codeprivate File[] struts; //文件,對應fileupload.jsp中input type=file的name private String[] strutsFileName; //文件名 private String[] strutsContentType; //文件類型 public File[] getStruts() { return struts; } public void setStruts(File[] struts) {this.struts = struts; } public String[] getStrutsFileName() { return strutsFileName; } public void setStrutsFileName(String[] strutsFileName) { this.strutsFileName = strutsFileName; } public String[] getStrutsContentType() { return strutsContentType; }public void setStrutsContentType(String[] strutsContentType) { this.strutsContentType = strutsContentType; } @Override public String execute() throws Exception { for(int i=0; i<struts.length; i++){ System.out.println("文件名:" + strutsFileName[i]); System.out.println("文件類型:" + strutsContentType[i]); String filePath = "E:/" + strutsFileName[i]; File savefile = new File(filePath); FileUtils.copyFile(struts[i], savefile); } return SUCCESS; } }
文件下載action
FileDownloadAction.java:
package com.cy.action; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class FileDownloadAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String fileName; //下載的文件名 public String getFileName() throws Exception { fileName=new String(fileName.getBytes(),"ISO8859-1"); //對文件中文名進行重編碼 return fileName; } public void setFileName(String fileName){ this.fileName = fileName; } /** * 返回的是文件流 * @return * @throws Exception */ public InputStream getInputStream() throws Exception{ File file = new File("E:/1.jpg"); //將E盤下的1.png下載下來 this.fileName = "美女哇哇.png"; //重新定義文件名 return new FileInputStream(file); //返回inputStream } }View Code
struts.xml:
<?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> <!-- 配置允許上傳文件最大為20000000Byte,約20Mb --> <constant name="struts.multipart.maxSize" value="20000000"></constant> <package name="manage" extends="struts-default"> <action name="upload" class="com.cy.action.FileUploadAction"> <result name="input">fileupload.jsp</result> <result name="success">success.jsp</result> <!-- 配置允許上傳的文件類型 配置允許上傳的文件大小,最大81101byte,= 79.2KB --> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param> <param name="maximumSize">81101</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> <action name="uploads" class="com.cy.action.FilesUploadAction"> <result name="input">filesupload.jsp</result> <result name="success">success.jsp</result> </action> <action name="download" class="com.cy.action.FileDownloadAction"> <result type="stream"> <param name="contentDisposition">attachment;filename=${fileName}</param> </result> </action> </package> </struts>
多個文件上傳filesupload.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="uploads" method="post" enctype="multipart/form-data"> 選擇文件1:<input type="file" name="struts" /><br/> 選擇文件2:<input type="file" name="struts" /><br/> 選擇文件3:<input type="file" name="struts" /><br/> <input type="submit" value="提交" /> </form> </body> </html>
文件上傳成功success.jsp:
<body> 文件上傳成功! </body>View Code
文件下載filedownload.jsp:
<body> <a href="download">文件下載</a> </body>
測試結果:
1.多個文件上傳:
選擇文件上傳,成功後console打印:
2.文件下載,將E盤下的1.jpg下載下來:
struts2學習(14)struts2文件上傳和下載(4)多個文件上傳和下載