标签:
Struts2控制文件上传与下载的几个注意事项:
(1)必须将表单的method设置为post,将enctype设置为multipart/from-data。只有这样,浏览器才会把用户选择文件的二进制数据发送给数据。
(2)Struts2默认使用的是Jakarta的Common-FileUpload的文件上传框架,因此,如果需要使用Struts2的文件上传功能,则需要在web应用中增加两个JAR文件,即commons-io-2.2.jar和commons-fileupload-1.3.1.jar。
一. 文件上传
(1)文件上传页面
1: <form action="upload" enctype="multipart/form-data" method="post">
2: <input name="upload" type="file">
3: <input type="submit" value="submit">
4: </form>
(2)处理上传请求的Action
1: public class FileUploadAction extends ActionSupport{
2: //封装上传文件域
3: private File upload;
4:
5: //上传文件类型
6: private String uploadContentType;
7:
8: //上传文件名
9: private String uploadFileName;
10:
11: //上传文件的保存路径,在Struts.xml中配置
12: private String savePath;
13:
14: public File getUpload() {
15: return upload;
16: }
17: public void setUpload(File upload) {
18: this.upload = upload;
19: }
20: public String getUploadContentType() {
21: return uploadContentType;
22: }
23: public void setUploadContentType(String uploadContentType) {
24: this.uploadContentType = uploadContentType;
25: }
26: public String getUploadFileName() {
27: return uploadFileName;
28: }
29: public void setUploadFileName(String uploadFileName) {
30: this.uploadFileName = uploadFileName;
31: }
32: public String getSavePath() {
33: return ServletActionContext.getServletContext().getRealPath(savePath);
34: }
35: public void setSavePath(String savePath) {
36: this.savePath = savePath;
37: }
38:
39: public String execute() throws IOException
40: {
41: //获取文件的输出流
42: FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
43:
44: //上传文件的输入流
45: FileInputStream fis = new FileInputStream(getUpload());
46: byte[] buffer = new byte[1024];
47: int len = 0;
48: while((len = fis.read(buffer)) > 0)
49: {
50: fos.write(buffer,0,len);
51: }
52: return SUCCESS;
53: }
54: }
(3)利用拦截器实现文件过滤
通过配置拦截器,可以方便地控制上传文件的类型,大小。如果文件类型不符合,则会返回input视图。Struts2中文件上传的拦截器是fileUpload。
配置fileUpload拦截器时,可以为其指定两个参数:
另外,特别注意,为了使fileUpload拦截器生效,需要显示在其之后配置defaultStack的拦截器。
Struts.xml 配置
1: <action name="upload" class="com.bigdata.action.FileUploadAction">
2: <interceptor-ref name="fileUpload">
3: <param name="allowedTypes">
4: application/octet-stream,application/zip,application/x-zip-compressed,
5: application/x-rar-compressed
6: </param>
7: <param name="maximumSize">20000000</param>
8: </interceptor-ref>
9: <interceptor-ref name="defaultStack" />
10: <param name="savePath">/data</param>
11: <result name="success">/jsp/index.jsp</result>
12: <result name="input">/uploadForm.jsp</result>
13: </action>
二. 文件下载
Struts2的文件下载Action与普通的Action并没有什么太大的不同,仅仅是该Action需要提供一个返回InputStream流的方法,该输入流代表了被下载文件的入口,同时在配置文件中配置Action的result类型为stream。
Action类的实现:
1: public class FileDownloadAction extends ActionSupport{
2: private String inputPath;
3: private String filename;
4:
5: public String getFilename() {
6: return filename;
7: }
8: public void setFilename(String filename) {
9: this.filename = filename;
10: }
11: public String getInputPath() {
12: return inputPath;
13: }
14: public void setInputPath(String inputPath){
15: this.inputPath = inputPath;
16: }
17: /*
18: 定义一个返回InputStream的方法
19: 该方法将被作为下载文件的入口,
20: 且需要配置Stream类型结果时指定inputName参数
21: inputName参数的值就是该方法去掉get前缀,首字母小写的字符串
22: */
23: public InputStream getTargetFile() throws IOException{
24: String path = ServletActionContext.getServletContext().getRealPath(inputPath);
25: //下载文件名如果包含中文需要转码,避免出现乱码
26: String name = new String(filename.getBytes("ISO8859-1"),"UTF-8");
27: //下载文件的路径
28: String filePath = path + "\\" + name;
29: File file = new File(filePath);
30: return FileUtils.openInputStream(file);
31: }
32:
33: //设置下载文件的名称
34: public String getDownloadName(){
35: String downloadName = filename;
36: try {
37: downloadName = new String(downloadName.getBytes(),"ISO8859-1");
38: } catch (UnsupportedEncodingException e) {
39: // TODO Auto-generated catch block
40: e.printStackTrace();
41: }
42: return downloadName;
43: }
44: }
Struts.xml的配置
配置下载文件的Action时,需要配置一个类型为stream的结果,需要指定如下4个属性。
1: <action name="download" class="com.bigdata.action.FileDownloadAction">
2: <param name="inputPath">/data</param>
3: <result type="stream" name="success">
4: <param name="contentType">application/octet-stream</param>
5: <param name="inputName">targetFile</param>
6: <param name="contentDisposition">attachment;filename="${downloadName}"</param>
7: </result>
8: <result name="login">/jsp/login.jsp</result>
9: </action>
三. 文件下载取消时出现异常
在struts2中使用result里type="stream"的结果类型时,当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常。
异常原因分析:
1. 配置其中result标签下的各个参数
2. 从服务器中获取输入流,并同时与客户端建立输出流(服务器与客户端链接通过Socket进行连接)
3. 当点击“保存”或“打开”时,开始传输数据。如果点击“取消”,关闭所有的流。这里要注意的是,但是实际发现Socket并没有断开!并且流也没有关闭!这一点非常重要!所以在JSP容器通过Response获取输出流之前,前面的流并没有关闭,所以会造成该异常的报出。
异常解决办法:
1. 下载插件struts2-sunspoter-stream-1.0.jar,并复制在/WEB-INF/lib下。
GitHub下载地址:https://github.com/41zone/StreamResultX
2. 在Struts.xml 中配置:
<result-types>
<result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX"/>
</result-types>
3. 在下载的Action中配置result的类型为streamx:
1: <action name="download" class="com.bigdata.action.FileDownloadAction">
2: <param name="inputPath">/data</param>
3: <result type="streamx" name="success">
4: <param name="contentType">application/octet-stream</param>
5: <param name="inputName">targetFile</param>
6: <param name="contentDisposition">attachment;filename="${downloadName}"</param>
7: </result>
8: <result name="login">/jsp/login.jsp</result>
9: </action>
标签:
原文地址:http://www.cnblogs.com/bloodhunter/p/4589772.html