上传:
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; } }
相关推荐
在struts2基础上实现的包括单文件、多文件的上传及下载
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接
Struts2中文件上传与下载案例
需求 1.能够对多个文件进行上传(可以选择上传文件个...提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载 随意文件 java Struts2 单例 配置 动态读取 李顺利
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
Struts2 文件上传下载 多文件 单文件 实例;支持单个或多个文件的上传下载,并且自定义上传文件的大小和类型!!!
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
使用struts2框架进行文件的上传并限制文件的大小与类型,使用struts2框架实现文件下载
struts2文件上传与下载, <s:fielderror></s:fielderror> 文件"></s:file> <s:submit id="submit" value="sbumit"></s:submit> </s:form>
struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!
Struts2文件上传下载和表单重复提交问题
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
struts实现的文件上传下载功能,最大可以上传到150MB
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
基于Struts2的文件上传下载功能的完整源代码。经过测试可正确运行。
使用struts1实现文件上传和下载,上传有单文件上传,多文件上传,动态添加上传文件域。
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...
struts2中的文件上传和下载示例 struts2中的文件上传和下载示例 struts2中的文件上传和下载示例