配置文件和Action文件:
struts.xml       
       <action name="download" class="xxx.xxx.action.FileDownloadAction">   
            <!-- 设置文件名参数,由页面上传入 -->   
            <param name="fileName"></param>   
            <result name="success" type="stream">   
                <!-- 下载文件类型定义 -->   
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>   
                <!-- 下载文件处理方法 -->   
                <param name="contentDisposition">   
                    attachment;filename="${downloadChineseFileName}"  
                </param>   
                <!-- 下载文件输出流定义 -->   
                <param name="inputName">downloadFile</param>   
            </result>   
            <interceptor-ref name="loginStack"></interceptor-ref>
        </action>  
FileDownloadAction.java
           public InputStream getDownloadFile() {
try {
//测试用
System.out.println("111111" + fileName);

                           //DOWNLOADFILEPATH  = "/upload/"
String dir = DOWNLOADFILEPATH + fileName;

System.out.println(dir);
InputStream is = ServletActionContext.getServletContext().getResourceAsStream(
dir);

return is;
} catch (Exception e) {
// TODO: handle exception

return null;
}


}
public String getDownloadChineseFileName() {
System.out.println("222222" + fileName);
String downloadChineseFileName = fileName; try {

downloadChineseFileName = new String(downloadChineseFileName
.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
                  //测试用
System.out.println("222222" + downloadChineseFileName);
return downloadChineseFileName;
}
问题描述: 1. 首先说明,下载非中文的文件是没有问题的。
          2. 点击我的JSP页面中的文件下载按钮后,能够正常的弹出下载提示框,并且其中的文件名也是中文。
          3. 问题在于,点击下载提示框中的下载按钮后,会发现系统不停的调用getDownloadFile()方法,貌似是试图建立连接?eclipse中循环不停的输出以下信息(《心理学的166个现象.docx》是我测试用的要下载的文件)。在这里,文件名变成了乱码,以至于找不到文件无法下载成功。不知道问题出在何处。 请各位朋友指教!
111111???????166??????.docx
100594 [DEBUG] com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler  - (Entering nullPropertyValue [target=[com.tellhow.action.FileDownloadAction@9bbca, com.opensymphony.xwork2.DefaultTextProvider@893543], property=downloadFile])
100594 [ERROR] org.apache.struts2.dispatcher.StreamResult  - (Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the <param name="inputName"> tag specified for this action.)110610 [DEBUG] com.opensymphony.xwork2.conversion.impl.InstantiatingNullHandler  - (Entering nullPropertyValue [target=[com.opensymphony.xwork2.DefaultTextProvider@893543], property=struts])
110610 [WARN ] com.opensymphony.xwork2.ognl.OgnlValueStack  - (Could not find property [struts.valueStack])
110610 [DEBUG] com.opensymphony.xwork2.DefaultActionProxy  - (Creating an DefaultActionProxy for namespace /jsp/page and action name download)
110610 [DEBUG] com.opensymphony.xwork2.interceptor.I18nInterceptor  - (intercept '/jsp/page/download' { )
110610 [DEBUG] com.opensymphony.xwork2.interceptor.I18nInterceptor  - (requested_locale=null)
110610 [DEBUG] com.opensymphony.xwork2.interceptor.I18nInterceptor  - (before Locale=zh_CN)
110610 [DEBUG] com.opensymphony.xwork2.interceptor.StaticParametersInterceptor  - (Setting static parameters {})
110610 [DEBUG] com.opensymphony.xwork2.interceptor.ParametersInterceptor  - (Setting params NONE)
110610 [DEBUG] com.opensymphony.xwork2.interceptor.ParametersInterceptor  - (Setting params  fileName => [ ???????166??????.docx ] )
110625 [DEBUG] com.opensymphony.xwork2.conversion.impl.XWorkConverter  - (Property: fileName)
110625 [DEBUG] com.opensymphony.xwork2.conversion.impl.XWorkConverter  - (Class: com.tellhow.action.FileDownloadAction)
110625 [DEBUG] com.opensymphony.xwork2.conversion.impl.XWorkConverter  - (field-level type converter for property [fileName] = none found)
110625 [DEBUG] com.opensymphony.xwork2.conversion.impl.XWorkConverter  - (global-level type converter for property [fileName] = none found)
110625 [DEBUG] com.opensymphony.xwork2.conversion.impl.XWorkConverter  - (falling back to default type converter [com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter@1ae0c29])
110625 [DEBUG] com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor  - (Invoking validate() on action com.tellhow.action.FileDownloadAction@16ab38f)
110625 [DEBUG] com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil  - (cannot find method [validateExecute] in action [com.tellhow.action.FileDownloadAction@16ab38f])
110625 [DEBUG] com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil  - (cannot find method [validateDoExecute] in action [com.tellhow.action.FileDownloadAction@16ab38f])
110625 [DEBUG] com.opensymphony.xwork2.DefaultActionInvocation  - (Executing action method = null)

解决方案 »

  1.   

    页面和tomcat都是UTF-8
    我一直以为页面和struts以及tomcat编码统一了就可以,难道是不行的啊?
    而且我第一次点我jsp页面的文件下载按钮时,eclipse输出的是111111心理学的166个现象.docx这时也没有乱码啊。为什么点击下载框里的下载后就出现乱码
      

  2.   


    肯定有咯,直接 return SUCCESS 罢了
      

  3.   

    charset=ISO8859-1   换成你页面配置的编码  程序里的也换掉 如果不行就组合换 先换一个在换另一个进行组合测试
      

  4.   

    要对文件名称进行转码
    package action;import java.io.InputStream;
    import java.io.UnsupportedEncodingException;import bean.FileInfo;import com.opensymphony.xwork2.ActionSupport;import dao.FileDAO;import org.apache.struts2.ServletActionContext;import util.OVLoadProperties;public class DownloadAction extends ActionSupport {
    private static final long serialVersionUID = 6329383258366253255L;
    private String fileName;
    private String fileSavename;
        private String fileId; 
        private FileDAO dao=new FileDAO();
    public String execute() throws Exception {
    return SUCCESS;
    }
    /*
     * @getDownloadFile 此方法对应的是struts.xml文件中的: <param
     * name="inputName">downloadFile</param> 返回下载文件的流,可以参看struts2的源码
     */
    public InputStream getDownloadFile() {
    FileInfo file=dao.findFileById(fileId);
    dao.addTimes(file.getFileTimes(),file.getFileId());
    return ServletActionContext.getServletContext().getResourceAsStream(
    "/"
    + OVLoadProperties.getInstance()
    .getProperties("uploadFilePath").trim() + "/"
    + fileSavename);
    } public String getFileSavename() {
    return fileSavename;
    } public void setFileSavename(String fileSavename)
    throws UnsupportedEncodingException {
    fileSavename = new String(fileSavename.getBytes("ISO-8859-1"), "gbk");
    this.fileSavename = fileSavename;
    } /*
     * @getFileName 此方法对应的是struts.xml文件中的: <param
     * name="contentDisposition">attachment;filename="${fileName}"</param>
     * 这个属性设置的是下载工具下载文件时显示的文件名, 要想正确的显示中文文件名,我们需要对fileName再次编码
     * 否则中文名文件将出现乱码,或无法下载的情况
     */
    public String getFileName() throws UnsupportedEncodingException { fileName = new String(fileName.getBytes(), "ISO-8859-1"); return fileName;
    } public void setFileName(String fileName)
    throws UnsupportedEncodingException {
    fileName = new String(fileName.getBytes("ISO-8859-1"), "gbk");
    this.fileName = fileName;
    }
    public void setFileId(String fileId) {
    this.fileId = fileId;
    }
    public String getFileId() {
    return fileId;
    }}