Struts2文件上传
1.单个文件上传:
a.首先建立一个jsp
<%@taglib uri="/struts-tags" prefix="s"%> <s:actionerror/><br/> <!--注意:添加命名空间、否则在第一次上传之后地址将发生改变--> <!--注意:2.enctype规定上传类型 否则上传文件在那头会出错--> <s:form action="upload" enctype="multipart/form-data" namespace="/"> <!--做标记用 用来判断是否重复登陆--> <s:token/> <s:file name="myFile" label="文件上传"/> <s:textfield name="name" label="用户名"/> <s:submit value="提交"/> </s:form>
注意:<s:token/>防止重复登陆,不能够返回之后重新提交。必须要刷新页面
b.Struts.xml配置
<package name="my" namespace="/" extends="struts-default"> <action name="upload" class="com.yc.struts.action.MyFileUploadAction"> <result>/success.jsp</result> </action> </package>
c.创建MyFileUploadAction类 继承ActionSupport
private File myFile;// 上传来的文件
private String myFileContentType;// 上传文件的类型
private String myFileFileName;// 上传文件的名字
实现get set方法
d.重写execute方法
public String execute() throws Exception { //1.获得输入流 InputStream in=new FileInputStream(myFile); //2.获得文件存储路径 //路径为C:\Program Files (x86)\apache-tomcat-7.0.47\webapps\struts2-mvc09-fileupload\upload //tomcat下webapps 项目名 File toDir=new File(ServletActionContext.getServletContext().getRealPath("/upload")); //3.判断是文件路径是否存在,不存在则创建文件 if(toDir.exists()==false){ toDir.mkdir(); } System.out.println(toDir.getAbsolutePath()); //4.输出流 OutputStream out=new FileOutputStream(new File(toDir,myFileFileName)); byte[] bs=new byte[1024]; int len=0; while((len=in.read(bs))!=-1){ out.write(bs,0,len); } out.flush(); in.close(); out.close(); return "success"; }
2.多个文件上传
方式一:
a.jsp
<s:form action="upload" enctype="multipart/form-data" namespace="/"> <s:token/> <s:file name="myFile" label="上传文件"/> <s:file name="myFile" label="上传文件"/> <s:file name="myFile" label="上传文件"/> </s:form>
b.struts.xml配置
<package name="my" namespace="/" extends="struts-default"> <action name="upload" class="com.yc.struts.action.MyFileUploadAction"> <result>/success.jsp</result> </action> </package>
c.创建MyFilesUploadAction类 继承ActionSupport
public class MyFilesUploadAction extends ActionSupport { //多中文件上传会 需要采用List<存> private List<File> myFile;// 上传来的文件 private List<String> myFileContentType;// 上传文件的类型 private List<String> myFileFileName;// 上传文件的名字 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; } @Override public String execute() throws Exception { InputStream in = null; OutputStream out = null; File toDir = new File(ServletActionContext.getServletContext() .getRealPath("/upload")); if (toDir.exists() == false) { toDir.mkdir(); } for (int i = 0; i < myFile.size(); i++) { in = new FileInputStream(myFile.get(i)); System.out.println(toDir.getAbsolutePath()); out = new FileOutputStream(new File(toDir, myFileFileName.get(i))); byte[] bs = new byte[1024]; int len = 0; while ((len = in.read(bs)) != -1) { out.write(bs, 0, len); } out.flush(); } in.close(); out.close(); return "success"; } }
3.多文件上传
方式二:使用Struts工具类 只需要更改execute()方法:
public String execute() throws Exception { System.out.println(myFile); System.out.println(myFileContentType); System.out.println(myFileFileName); File toDir = new File(ServletActionContext.getServletContext().getRealPath("/upload")); if (toDir.exists() == false) { toDir.mkdir(); } for (int i = 0; i < myFile.size(); i++) { //struts2框架中提供的文件操作帮助类 FileUtils.copyFile(myFile.get(i), new File(toDir,myFileFileName.get(i))); } return "success"; }
4. 上传参数控制
<package name="my" namespace="/" extends="struts-default"> <action name="upload" class="com.yc.struts.action.MyFilesUploadAction"> <interceptor-ref name="token"/> <interceptor-ref name="defaultStack"> <param name="fileUpload.maximumSize">2097512</param><!--允许上传的文件大小--> <param name="fileUpload.allowedTypes">text/plain,text/html</param><!-- 允许的文件类型 --> <param name="fileUpload.allowedExtensions">txt,html,htm,log</param><!-- 允许的文件拓展名 --> </interceptor-ref> <result>/success.jsp</result> <result name="input">/index.jsp</result> </action> </package>
5.下载
a.配置struts.xml
<action name="down" class="com.yc.struts.action.MyDownloadAction"> <result type="stream"> <param name="bufferSize">1024</param> </result> </action>
注意:此处标记 结果类型为 stream
b.创建MyDownloadAction
package com.yc.struts.action; import java.io.FileInputStream; import java.io.InputStream; import java.net.URLEncoder; import org.apache.struts2.ServletActionContext; public class MyDownloadAction { protected String contentType;//下载内容类型 protected long contentLength;//下载内容长度 protected String contentDisposition;//下载内容配置(名字) protected InputStream inputStream;//输入流 public String execute() throws Exception{ contentType="text/html"; //contentDisposition="attachment;filename="+new String("你好.html".getBytes(), "iso-8859-1"); contentDisposition="attachment;filename="+URLEncoder.encode("你好.html", "iso-8859-1");//转码 firebug不会识别这个utf-8 String downloadPath=ServletActionContext.getServletContext().getRealPath("/upload/a.html"); inputStream=new FileInputStream(downloadPath); contentLength=inputStream.available(); return "success"; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public long getContentLength() { return contentLength; } public void setContentLength(long contentLength) { this.contentLength = contentLength; } public String getContentDisposition() { return contentDisposition; } public void setContentDisposition(String contentDisposition) { this.contentDisposition = contentDisposition; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } }
原文:http://my.oschina.net/yedianxiaoxinxin/blog/299611