上传:
Action:
package com.tch.test.template.web.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.io.IOException; import com.opensymphony.xwork2.ActionSupport; @Component("uploadFileAction") @Scope("prototype") public class UploadFile extends ActionSupport { private static final long serialVersionUID = 1L; private File myFile; private String myFileContentType; private String myFileFileName; private String destPath; public String uploadFile() { /* Copy file to a safe location */ destPath = "e:/"; try { System.out.println("Src File name: " + myFile); System.out.println("Dst File name: " + myFileFileName); File destFile = new File(destPath+myFileFileName); FileUtils.copyFile(myFile, destFile); } catch (IOException e) { e.printStackTrace(); return ERROR; } return SUCCESS; } public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } public String getMyFileContentType() { return myFileContentType; } public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType; } public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } }
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/upload" extends="struts-default"> <!-- 进入上传页面 --> <action name="index"> <result> /upload/upload.jsp </result> </action> <!-- 处理文件上传 --> <action name="uploadFile" class="uploadFileAction" method="uploadFile"> <interceptor-ref name="fileUpload"> <param name="maximumSize">7000000</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="success"> /upload/uploadSuccess.jsp </result> <result name="input"> /upload/uploadError.jsp </result> </action> </package> </struts>
struts.properties:
#国际化字符编码 struts.i18n.encoding=UTF-8 #处理请求的后缀格式 struts.action.extension=htm,action #开发模式 struts.devMode=true #是否允许动态方法调用 struts.enable.DynamicMethodInvocation=true #限制最大文件上传大小 struts.multipart.maxSize=10485760
上面限制上传文件大小的方式二选一:
一:要么通过在action中加入拦截器设置来控制:
<!-- 处理文件上传 --> <action name="uploadFile" class="uploadFileAction" method="uploadFile"> <interceptor-ref name="fileUpload"> <param name="maximumSize">7000000</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <result name="success"> /upload/uploadSuccess.jsp </result> <result name="input"> /upload/uploadError.jsp </result> </action>
二:要么通过在struts.properties中配置:
#国际化字符编码 struts.i18n.encoding=UTF-8 #处理请求的后缀格式 struts.action.extension=htm,action #开发模式 struts.devMode=true #是否允许动态方法调用 struts.enable.DynamicMethodInvocation=true #限制最大文件上传大小 struts.multipart.maxSize=10485760
上传页面 upload.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="upload/uploadFile.htm" method="post" enctype="multipart/form-data"> <label for="myFile">选择文件:</label> <input type="file" name="myFile" /> <input type="submit" value="上传"/> </form> </body> </html>
上传成功:uploadSuccess.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'uploadSuccess.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 文件 <s:property value="myFileFileName"/> 上传成功! </body> </html>
如果文件大小超过限制大小,会进入result为 input 的页面:
上传失败:uploadError.jsp:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>File Upload Error</title> </head> <body> 上传失败. </body> </html>
多文件上传:
差别不大,首先是页面(多个相同的input file ,name相同):
upload.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="upload/uploadFile.htm" method="post" enctype="multipart/form-data"> <label for="myFile">选择文件:</label> <input type="file" name="myFile" /><br> <input type="file" name="myFile" /><br> <input type="submit" value="上传"/> </form> </body> </html>
后台action里面接受文件的变量也使用集合:
package com.tch.test.template.web.action; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.commons.io.FileUtils; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.opensymphony.xwork2.ActionSupport; @Component("uploadFileAction") @Scope("prototype") public class UploadFile extends ActionSupport { private static final long serialVersionUID = 1L; private List<File> myFile; private List<String> myFileContentType; private List<String> myFileFileName; private String destPath; public String uploadFile() { /* Copy file to a safe location */ destPath = "e:/"; try { // System.out.println("Src File name: " + myFile); System.out.println("Dst File name: " + myFileFileName); File srcFile = null; File destFile = null; int length = myFile.size(); for(int i=0;i<length;i++){ srcFile = myFile.get(i); destFile = new File(destPath+myFileFileName.get(i)); FileUtils.copyFile(srcFile, destFile); } } catch (IOException e) { e.printStackTrace(); return ERROR; } return SUCCESS; } public List<File> getMyFile() { return myFile; } public void setMyFile(List<File> myFile) { this.myFile = myFile; } public List<String> getMyFileContentType() { return myFileContentType; } public void setMyFileContentType(List<String> myFileContentType) { this.myFileContentType = myFileContentType; } public List<String> getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(List<String> myFileFileName) { this.myFileFileName = myFileFileName; } }
其它的都一样。
下载:
struts.xml:
<package name="download" namespace="/" extends="struts-default"> <!-- 下载--> <action name="download" class="uploadFileAction" method="download"> <result type="stream"> <param name="contentType">application/octet-stream;charset=ISO8859-1</param> <param name="inputName">myInputStream</param> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">4096</param> </result> </action> </package>
inputName指定获取文件流的方式,例如myInputStream的话,就需要action里面有getMyInputStream的方法
contentDisposition指定下载文件的名字,这里通过后台的方法返回文件名,解决中文的问题:
package com.tch.test.template.web.action; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.opensymphony.xwork2.ActionSupport; @Component("uploadFileAction") @Scope("prototype") public class UploadFile extends ActionSupport { private static final long serialVersionUID = 1L; private String downloadFileName; public String download(){ return SUCCESS; } public InputStream getMyInputStream()throws Exception{ return ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/中文名.doc"); } public String getDownloadFileName() { try { downloadFileName = new String("中文名.doc".getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return downloadFileName; } public void setDownloadFileName(String downloadFileName) { this.downloadFileName = downloadFileName; } }