Struts2中支持文件上传与下载的两个JAR文件是:
commons-fileupload-1.2.2.jar
commons-io-1.4.jar,
这两个包要到apache网站上单独下载,下载网址是:
http://commons.apache.org/fileupload/download_fileupload.cgi,
可下载:commons-fileupload-1.2.2-bin.zip,解压后,在它的lib中有commons-fileupload-1.2.2.jar
http://commons.apache.org/io/download_io.cgi,
可下载:commons-io-1.4-bin.zip,解压后,有commons-io-1.4.jar
要上传文件需要用到标签:<s:file>
还要用到表单的“enctype”属性,设置为:enctype="multipart/form-data" ,传送二进制数据文件。
Demo:
1、首页 index.jsp
1 <%@ page language="java" contentType="text/html;charset=UTF-8"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 <html> 4 <head> 5 <title>上传文件</title> 6 </head> 7 <body vLink="#006666" link="#003366" bgColor="#E0F0F8"> 8 <br> 9 <b>上传文件</b> 10 <s:form action="upload" enctype="multipart/form-data" method="post"> 11 <s:file name="upload" label="选择文件1"> 12 </s:file> 13 <s:file name="upload" label="选择文件2"> 14 </s:file> 15 <s:file name="upload" label="选择文件3"> 16 </s:file> 17 <s:submit value="上传"></s:submit> 18 </s:form> 19 </body> 20 </html>
2、struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 8 <constant name="struts.devMode" value="true" /> 9 10 <package name="up-load" extends="struts-default"> 11 <action name="upload" class="com.action.UpLoadAction"> 12 <!-- 部署本工程前,要在WebRoot下手工创建文件夹upload,上传保存到tomcat容器的webapps中的本工程的upload文件夹中 --> 13 <param name="savePath">upload</param> 14 <!-- 上传失败时返回的视图页面 --> 15 <result name="success">upsuccess.jsp</result> 16 <result name="input">uperror.jsp</result> 17 <!-- 声明action使用的拦截器 fileUpload --> 18 <interceptor-ref name="fileUpload"> 19 <!-- 限制上传的文件类型 --> 20 <param name="allowedTypes">image/gif,image/jpeg,image/x-png,image/bmp,text/plain,application/msword,application/vnd.ms-excel,application/x-zip-compressed,application/pdf,application/vnd.ms-powerpoint</param> 21 <!-- 限制上传文件大小为2048000字节(2M) --> 22 <param name="maximumSize">2048000</param> 23 </interceptor-ref> 24 <!-- 配置Struts 2 的默认拦截器栈 --> 25 <interceptor-ref name="defaultStack" /> 26 </action> 27 </package> 28 </struts> 29 30 <!-- 对上传类型的说明: --> 31 <!-- gif文件:image/gif --> 32 <!-- jpg文件:image/jpeg --> 33 <!-- png文件: image/x-png --> 34 <!-- bmp文件:image/bmp --> 35 <!-- txt文件:text/plain --> 36 <!-- doc文件:application/msword --> 37 <!-- xls文件:application/vnd.ms-excel --> 38 <!-- zip、jar文件: application/x-zip-compressed --> 39 <!-- pdf文件:application/pdf --> 40 <!-- ppt文件:application/vnd.ms-powerpoint --> 41 <!-- 除上述类型外的其它文件类型:application/octet-stream -->
3、Action代码
1 package com.action; 2 3 import java.io.*; 4 import org.apache.struts2.ServletActionContext; 5 import com.opensymphony.xwork2.ActionSupport; 6 7 public class UpLoadAction extends ActionSupport { 8 private File[] upload; // 封装上传文件 9 private String[] uploadContentType; // 封装上传文件类型 10 private String[] uploadFileName; // 封装上传文件名 11 private String savePath; // 封装上传文件在服务器tomcat容器上的保存路径 12 13 public File[] getUpload() { 14 return upload; 15 } 16 17 public void setUpload(File[] upload) { 18 this.upload = upload; 19 } 20 21 public String[] getUploadContentType() { 22 return uploadContentType; 23 } 24 25 public void setUploadContentType(String[] uploadContentType) { 26 this.uploadContentType = uploadContentType; 27 } 28 29 public String[] getUploadFileName() { 30 return uploadFileName; 31 } 32 33 public void setUploadFileName(String[] uploadFileName) { 34 this.uploadFileName = uploadFileName; 35 } 36 37 public String getSavePath() { 38 // 以下ServletActionContext.getServletContext().getRealPath("")方法获取本工程在tomcat容器中的真实绝对路径,加上参数savepath,进一步得到下一级savepath所代表的文件夹的绝对路径 39 String realpath = ServletActionContext.getServletContext().getRealPath(savePath); // 应为使用的是2.0的ServletAPI,所以不能调用request.getRealPath() 40 return realpath; 41 } 42 43 public void setSavePath(String savePath) { 44 this.savePath = savePath; 45 } 46 47 public String execute() throws Exception { 48 File[] files = getUpload(); 49 for (int i = 0; i < files.length; i++) { 50 FileInputStream fileInput = new FileInputStream(files[i]); // 创建文件输入流 51 FileOutputStream fileOutput = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()[i]); // 创建文件输出流 52 byte[] b = new byte[2048]; // 缓冲数组大小2KB 53 int len = 0; 54 while ((len = fileInput.read(b)) > 0) // finout.read(b)实现从输入流读1KB数据到数组b中,返回读取的字节数len,遇到文件尾返回-1 55 { 56 fileOutput.write(b, 0, len); // 将b中len字节数据写到输出流中 57 } 58 //fileInput.close(); // 关闭输入流 59 //fileOutput.close(); // 关闭输出流 60 } 61 return "success"; 62 } 63 }
4、upsuccess.jsp
1 <%@ page language="java" contentType="text/html;charset=UTF-8" %> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 <html> 4 <head> 5 <title>上传文件成功</title> 6 </head> 7 <body vLink="#006666" link="#003366" bgColor="#E0F0F8"> 8 <img src="img/smile.gif"> 9 <h3>上传文件成功:</h3> 10 <s:property value="uploadFileName"/><br><br> 11 <a href="index.jsp">继续上传</a> 12 <s:debug></s:debug> 13 </body> 14 </html>
4、uperror.jsp
1 <%@ page contentType="text/html;charset=UTF-8" language="java"%> 2 <%@ taglib prefix="s" uri="/struts-tags"%> 3 <html> 4 <head> 5 <title>查询失败</title> 6 </head> 7 <body vLink="#006666" link="#003366" bgColor="#E0F0F8"> 8 <img src="img/cry.gif"> 9 <h1>上传失败!</h1><br><br> 10 <s:fielderror></s:fielderror> 11 <br><br> 12 <a href="index.jsp">返回上传页面</a> 13 </body> 14 </html>
PS:
若上传后文件名乱码,只需将页面的编码方式和struts.xml的设置的编码方式都用成gbk 即可
Struts.xml的配置文件<constant name="struts.i18n.encoding" value="gbk"></constant>
struts2 jpg文件上传失败 Content-Type not allowed:
原因:ie会把 jpg、jpeg翻译成image/pjpeg,png翻译成image/x-png 。而火狐则很标准:jpg、jpeg翻译成image/jpeg,png翻译成image/png。
解决方法:用火狐、谷歌等浏览器 or
<param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg </param>