zoukankan      html  css  js  c++  java
  • Struts2学习9--中文文件下载的问题

    这个似乎是两个问题

    1)中文文件内容乱码

    2)中文文件名是乱码或文件名为空

    第一个问题我的解决方法是,全都用utf-8编码。包括

    Tomcat里的server.xml

        <Connector port="8080" protocol="HTTP/1.1" 
                   connectionTimeout="20000" 
                   redirectPort="8443"  URIEncoding="UTF-8"/>

    struts2里设置

        <constant name="struts.il8n.encoding" value="utf-8"/> 

    JSP里设置

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%


    第二个问题的解决方法:

    在action的配置中,inputName参数是下载文件流的名称(InputStream的名称),conentDisposition中的文件名(downloadfileName)是实际下载的时候文件的名称。所以文件名称不是中文的问题,应该在这里设置的时候,转换编码。

                <action name="downloadZH"  class="com.meetcomet.util.FileDownLoadZHAction">
                <param name="path">/download</param>
                <result name="success" type="stream">
                    <param name="contentType">
                        text/plain
                    </param>
                    <!-- 文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。 -->
                    <param name="contentDisposition">
                        attachment;filename=${downloadfileName}
                    </param>
                    <param name="inputName">targetFile</param>
                    <!-- 缓冲区大小 -->
                    <param name="bufferSize">2048</param>
                </result>
                <result name="error">/fail.jsp</result>
            </action>

    对应的action如下

    中文名的问题,要特别注意downloadfileName的get函数。其余和非中文的相比,并无特别变化

     1 public class FileDownLoadZHAction extends ActionSupport {
     2 
     3       /**
     4       * @return
     5       */
     6       private String fileName;  
     7         private String path;  
     8         private InputStream targetFile;  
     9        private  String downloadfileName;
    10         
    11       public String getFileName() {
    12          return fileName;
    13       }
    14 
    15       public void setFileName(String fileName) {
    16          this.fileName = fileName;
    17       }
    18 
    19       public String getPath() {
    20          return ServletActionContext.getServletContext().getRealPath(path);
    21       }
    22 
    23       public void setPath(String path) {
    24          this.path = path;
    25       }
    26 
    27 
    28 
    29       public String execute() {
    30            String filePath=getPath()+ "\" +getFileName() ;  
    31            System.out.println(filePath);
    32             try {  
    33                 targetFile=new FileInputStream(filePath);  
    34                 return SUCCESS;  
    35            } catch (FileNotFoundException e) {  
    36                e.printStackTrace();  
    37                 return ERROR;  
    38            }  
    39         
    40       }
    41 
    42 
    43 
    44     public String getDownloadfileName() {
    45          try {  
    46              downloadfileName = new String(this.fileName.getBytes(), "ISO8859-1");   
    47              System.out.println(downloadfileName);
    48           } catch (UnsupportedEncodingException e) {  
    49              e.printStackTrace();
    50           }  
    51          return downloadfileName;
    52     }
    53 
    54 
    55 
    56     public InputStream getTargetFile() {
    57         return targetFile;
    58     }
    59 
    60     public void setTargetFile(InputStream targetFile) {
    61         this.targetFile = targetFile;
    62     }
    63 
    64     public void setDownloadfileName(String downloadfileName) 
    65     {
    66         this.downloadfileName=downloadfileName;
    67     }
    68 
    69 
    70 
    71     }
    View Code
  • 相关阅读:
    旧文备份:CANopen协议PDO的几种传输方式
    CANopen 基础
    单片机FLASH与RAM、ROM的关系
    在CANopen网络中通过LSS服务设置节点地址和网络波特率
    STM32F103 CAN中断发送功能的再次讨论
    由RS-232串口到PROFIBUS-DP总线的转换接口设计
    profibus 的DPV0 和DPV1
    PROFIBUS-DP
    profibus总线和profibus dp的区别
    获取验证码倒计时
  • 原文地址:https://www.cnblogs.com/meetcomet/p/3418515.html
Copyright © 2011-2022 走看看