struts2 的文件上传与下载

来源:互联网 发布:网络连接错误807 编辑:程序博客网 时间:2024/05/14 19:40
本文主要写的是struts2 的文件上传与下载(中文文件名问题的解决)。

使用的时候直接在地址栏中输入:http://localhost:8080/fud/ 回车即可

详细配置见下面代码:
struts.xml文件的配置
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.     <!-- 设置默认编码格式 -->  
  5.     <constant name="struts.i18n.encoding" value="GBK" />  
  6.     <!-- 设置最大上传文件是300M -->  
  7.     <constant name="struts.multipart.maxSize" value="314572800" />  
  8.     <!-- 设置默认的临时文件存储位置 -->  
  9.     <constant name="struts.multipart.saveDir" value="C:/fileUpload" />  
  10.     <!-- 设置调试模式 -->  
  11.     <constant name="struts.devMode" value="true"></constant>  
  12.     <package name="fileupload" extends="struts-default" namespace="/file">  
  13.         <!-- 文件上传 -->  
  14.         <action name="upload" class="com.file.action.FileUploadAction">  
  15.             <result type="redirectAction">  
  16.                 <param name="actionName">list</param>  
  17.                 <param name="namespace">/file</param>  
  18.             </result>  
  19.         </action>  
  20.         <!-- 下载文件 -->  
  21.         <action name="download" class="com.file.action.FileDownloadAction">  
  22.             <result type="stream" name="success">  
  23.                 <param name="contentType">application/octet-stream</param>  
  24.                 <!-- 要有相对应的getDownloadFile()方法返回值是 InputStream -->  
  25.                 <param name="inputName">downloadFile</param>  
  26.                 <param name="contentDisposition">attachment;filename="${fileName}"</param>  
  27.                 <param name="bufferSize">4096</param>  
  28.             </result>  
  29.         </action>  
  30.         <!-- 文件下载列表 -->  
  31.         <action name="list" class="com.file.action.FileListAction">  
  32.             <result>/file.jsp</result>  
  33.         </action>  
  34.     </package>  
  35. </struts>      


上传文件Action处理:
上传的时候到不用注意中文编码问题,因为struts已经帮我们做了
Java代码  收藏代码
  1. package com.file.action;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.apache.commons.io.FileUtils;  
  6.   
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.   
  9. public class FileUploadAction extends ActionSupport {  
  10.     private File file;  
  11.     private String contentType;  
  12.     private String fileName;  
  13.   
  14.     @Override  
  15.     public String execute() throws Exception {  
  16.         File saveFile = new File("c:/download/" + fileName);  
  17.         if (!saveFile.getParentFile().exists())  
  18.             saveFile.getParentFile().mkdirs();  
  19.         FileUtils.copyFile(file, saveFile);// 复制文件  
  20.         this.addFieldError("isSuccess""文件上传成功!");  
  21.         return SUCCESS;  
  22.     }  
  23.   
  24.     public void setUploadContentType(String contentType) {  
  25.         this.contentType = contentType;  
  26.     }  
  27.   
  28.     public void setUpload(File file) {  
  29.         this.file = file;  
  30.     }  
  31.   
  32.     public void setUploadFileName(String fileName) {  
  33.         this.fileName = fileName;  
  34.     }  
  35. }  


下载时的Action,这个需要特别的注意:特别是在下载带有中文名字的文件的时候要对中文进行编码与解码。代码如下
Java代码  收藏代码
  1. package com.file.action;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.InputStream;  
  6. import java.io.UnsupportedEncodingException;  
  7.   
  8. import com.opensymphony.xwork2.ActionSupport;  
  9.   
  10. public class FileDownloadAction extends ActionSupport {  
  11.     private String fileName;// 要下载的文件名  
  12.   
  13.     public String execute() throws Exception {  
  14.         return SUCCESS;  
  15.     }  
  16.   
  17.     // 下载文件  
  18.     public InputStream getDownloadFile() {  
  19.         InputStream is = null;  
  20.         try {  
  21.             is = new FileInputStream("c:/download/" + fileName);  
  22.         } catch (FileNotFoundException e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         return is;  
  26.     }  
  27.   
  28.     public void setFileName(String fileName) {  
  29.         try {// 解决中文文件名问题  
  30.             this.fileName = new String(fileName.getBytes("ISO-8859-1"), "GBK");  
  31.         } catch (UnsupportedEncodingException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35.   
  36.     public String getFileName() {  
  37.         String name = "";  
  38.         try {// 解决下载文件中文文件名问题  
  39.             name = new String(fileName.getBytes("GBK"), "ISO8859-1");  
  40.         } catch (UnsupportedEncodingException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         return name;  
  44.     }  
  45. }  

下面这个与文件下载就没有什么关系了,主要是为了显示主页面用的:代码如下
Java代码  收藏代码
  1. package com.file.action;  
  2.   
  3. import java.io.File;  
  4. import java.util.LinkedList;  
  5. import java.util.List;  
  6.   
  7. import com.opensymphony.xwork2.ActionSupport;  
  8.   
  9. public class FileListAction extends ActionSupport {  
  10.     private List<File> files = new LinkedList<File>();  
  11.   
  12.     public String execute() throws Exception {  
  13.         return SUCCESS;  
  14.     }  
  15.   
  16.     public List<File> getFiles() {  
  17.         File file = new File("c:/download/");  
  18.         File filelist[] = file.listFiles();  
  19.         for (File f : filelist) {  
  20.             files.add(f);  
  21.         }  
  22.         return files;  
  23.     }  
  24. }  

接下来就是jsp页面了;
导航页面index.jsp写法
Js代码  收藏代码
  1. <script language="javascript">  
  2.     window.location = "/fud/file/list";  
  3.      </script>  


在这就是显示页面了 list.jsp
Java代码  收藏代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%@taglib uri="/struts-tags" prefix="s"%>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.     <head>  
  6.     </head>  
  7.     <body>  
  8.         <h3>  
  9.             下载文件  
  10.         </h3>  
  11.         <table align="center" border="1">  
  12.             <tr>  
  13.                 <td width="10%">  
  14.                     序号  
  15.                 </td>  
  16.                 <td width="80%">  
  17.                     文件名  
  18.                 </td>  
  19.                 <td width="10%">  
  20.                     下载  
  21.                 </td>  
  22.             </tr>  
  23.             <tr>  
  24.                 <td>  
  25.                     <s:iterator status="status" value="files" var="f">  
  26.                         <tr>  
  27.                             <td>  
  28.                                 <s:property value="#status.count" />  
  29.                             </td>  
  30.                             <td>  
  31.                                 <s:property value="#f.name" />  
  32.                             </td>  
  33.                             <td>  
  34.                                 <s:a action="download" namespace="/file" encode="false">  
  35.                                     <s:param name="fileName" value="%{#f.name}" />  
  36.                                 下载  
  37.                                 </s:a>  
  38.                             </td>  
  39.                         </tr>  
  40.                     </s:iterator>  
  41.         </table>  
  42.         <hr>  
  43.         <!-- 文件上传模块 -->  
  44.         <s:form action="upload" method="post" enctype="multipart/form-data" namespace="/file">  
  45.             <s:file label="请选择文件" name="upload" ></s:file>  
  46.             <s:submit name="submit" value="提交"></s:submit>  
  47.             <s:fielderror name="isSuccess" />  
  48.         </s:form>  
  49.         <s:debug></s:debug>  
  50.     </body>  
  51. </html>  

使用说明:
在浏览器中输入:http://localhost:8080/fud
结果如图(当然前提是"c:\\download\"文件夹下要有东西)



运行后的界面图:



下载非中文文件名的文件:



下载中文文件:


转自:http://aimilin6688.iteye.com/blog/1062730

原创粉丝点击