struts2文件上传下载(含中文编码问题)

1.上传界面jsp

enctype属性一定要写成multipart/form-data不然就会以二进制文本上传到服务器端,使用 get/post方法对中文编码转码有影响 

[html]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. <form enctype="multipart/form-data" action="" method="post">  
  2.     <input type="file" id="choosefile" />  
  3.     <input type="file"  id="f" style="display:none;" />  
  4.     <input type="submit" value="上传文件" id="submitBtn" />  
  5. </form>  

2.文件上传

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. public class FileUploadAction extends ActionSupport  
  2.     {   <p>           <span style="color:#009900;">//file<span style="font-family:宋体;">是文件上传过来存放在临时文件夹下面的文件</span></span>  
  3. </p>      private File file;  
  4.           
  5.        <span style="color:#009900;">//提交过来的file的名字</span>  
  6.       private String fileFileName;  
  7.           
  8.         <span style="color:#009900;">//提交过来的file的MIME类型</span>  
  9.         private String fileContentType;  
  10.   
  11.            public File getFile()  
  12.         {  
  13.             return file;  
  14.         }  
  15.   
  16.         public void setFile(File file)  
  17.         {  
  18.             this.file = file;  
  19.         }  
  20.   
  21.         public String getFileFileName()  
  22.         {  
  23.             return fileFileName;  
  24.         }  
  25.   
  26.         public void setFileFileName(String fileFileName)  
  27.         {  
  28.             this.fileFileName = fileFileName;  
  29.         }  
  30.   
  31.         public String getFileContentType()  
  32.         {  
  33.             return fileContentType;  
  34.         }  
  35.   
  36.         public void setFileContentType(String fileContentType)  
  37.         {  
  38.             this.fileContentType = fileContentType;  
  39.         }  
  40.           
  41.         @Override  
  42.         public String execute() throws Exception  
  43.         //ServletActionContext.getServletContext().getRealPath()获得文件所在文件夹的相对路径在WebRoot下事先建立upload文件夹,上传的文件临时存在这里  
  44.             String root = ServletActionContext.getServletContext().getRealPath("/upload");  
  45.               
  46.             InputStream is = new FileInputStream(file);  
  47.               
  48.             OutputStream os = new FileOutputStream(new File(root, fileFileName));  
  49.             byte[] buffer = new byte[500];  
  50.             while(-1 != (is.read(buffer, 0, buffer.length)))  
  51.             {  
  52.                 os.write(buffer);  
  53.             }  
  54.               
  55.             os.close();  
  56.             is.close();  
  57.               
  58.                      }  
  59.                  }  


3.struts.xml上传配置

[html]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. <!--要设置struts文件上传的大小,这里是1048576000/(1024*1024)=10M之前没设置大文件上传时服务器直接重置-->  
  2. <constant name="struts.multipart.maxSize" value="1048576000"/>  
  3. <action name="addVideo" class="com.scholar.action.VideoServiceAction">  
  4.       <!-- fileUpload拦截器配置 -->  
  5.       <interceptor-ref name="fileUpload">  
  6.   
  7.       <!--设置上传文件最大字节数10M -->  
  8.          <param name="maximumSize">10485760</param>  
  9.       </interceptor-ref>  
  10.       <interceptor-ref name="defaultStack"/>  
  11.   
  12.      <!-- 设置上传文件的保存位置 -->  
  13.       <param name="savePath">upload</param>  
  14.       <result name="success">Success.jsp</result>  
  15.       <result name="input">Error.jsp</result>  
  16.    </action>  


注意:

        1. jsp中只有name="file"的一个元素,而action中却要得到三个数据 file,fileFileName,fileContentType,这是struts 2中文件上传机制自行实现的。

         2.上传文件过程中upload只是临时存放文件的作用,文件上传完毕后会删除数据,所以,在myeclipse的WebRoot下的upload文件中任何时候看都是没有任何文件的,那么上传的文件在哪?

       其实是在Tomcat中,打开路径:apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\webapps\scholar\upload

       原来文件最终是上传到了Tomcat服务器中,只有这里有文件才能算是上传成功

       项目的目的是将文件路径上传到了数据库中(代码省略),文件其实存在了服务器中。

4.下载界面jsp

[plain]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. //动态获取相对应的文件,赋值给fileName  
  2. <li>${fileFileName}<a href="fileDownload.action?fileName=${uploadFileName}">下载</a></li>  

5.下载文件(中文命名文件未完全解决,但有试调过程,知道了些东西分享下)

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. public class FileDownload extends ActionSupport{  
  2.     private String fileName;// 初始的通过param指定的文件名属性  
  3.   
  4.     private String inputPath;// 指定要被下载的文件路径  
  5.   
  6.   
  7.     public InputStream getInputStream() throws Exception {  
  8.   
  9.     // 通过 ServletContext,也就是application 来读取数据  
  10.   
  11.         return ServletActionContext.getServletContext().getResourceAsStream(inputPath+"\\"+fileName);   
  12.           
  13.     }  
  14.   
  15.     public String execute() throws Exception {  
  16.   
  17.     return SUCCESS;  
  18.   
  19.     }  
  20.   
  21.     public void setInputPath(String value) {  
  22.   
  23.     inputPath = value;  
  24.   
  25.     }  
  26.   
  27.     public void setFileName(String fileName) {     
  28.   
  29.         this.fileName = fileName;  
  30.     }  
  31.   
  32.     public String getFileName() {  
  33.   
  34.         return this.fileName;  
  35.   
  36.     }  
  37. }  


6.struts.xml

[html]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. <action name="fileDownload" class="com.scholar.action.FileDownload">  
  2.    <!-- 设置文件所在位置 -->  
  3.       <param name="inputPath">/upload</param>  
  4.     
  5.  <!-- 下载显示的文件名称 -->   
  6.             <param name="fileName">${fileName}</param>   
  7.             <result name="success" type="stream">    
  8.                <!-- 指定下载文件的文件类型 -->  
  9.                 <param name="contentType">application/octet-stream;</param>    
  10.                 <!-- 指定stream方法名称 -->  
  11.                 <param name="inputName">inputStream</param>    
  12.                <!-- 文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,默认为直接显示文件 -->  
  13.                 <param name="contentDisposition">attachment;fileName="${fileName}" </param>  
  14.                <!-- 指定下载文件的缓冲大小 -->         
  15.                 <param name="bufferSize">40960</param>    
  16.             </result>    

注意:

1.英文的文件上传没有问题,但是中文上传就报错:

Can not find a Java.io.InputStream with the name [InputStream] in the invocation stack.Check the<param name="inputName'> tag specified for this action.

翻译过来以为是inputName的设置问题,其实也可能是中文命名的文件在Tomcat的upload文件夹下找不到要下载的文件。

测试方法:打印getInpuStream的返回值

[java]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. public void getInputStream() throws Exception {  
  2.      InputStream in = ServletActionContext.getServletContext().getResourceAsStream(inputPath+"\\"+fileName);   
  3.      System.out.println("\n in = "+in);  
  4. }  
如果 in = null 则表示找不到文件,可能是路径错误或者找不到文件(可能就是编码问题)

编码问题原因:项目中所有文件编码都使用UTF-8编码,但是Tomcat传输的编码默认是ISO8859-1编码,编码方式不一致导致文件名匹配不成功找不到文件。

打开文件:apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\conf \ server.xml

没有URIEncoding="UTF-8"属性的话就是默认的ISO8859-1编码,手动添加:

[html]   view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. <Connector port="8080" protocol="HTTP/1.1"  
  2.     connectionTimeout="20000"  
  3.     redirectPort="8443" URIEncoding="UTF-8"/>  

这样中文命名的文件也可以正确匹配并且下载,但是在下载框中中文会显示为空,会显示英文部分。

最近又知道MyEclipse中Java文件默认的编码是GBK

意思是:

jsp编码:UTF-8(默认是ISO8859-1)

Java编码:GBK(默认)

Tomcat传输编码:ISO8859-1(默认)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值