码迷,mamicode.com
首页 > 其他好文 > 详细

解决struts2文件下载中文名问题

时间:2014-07-21 10:18:38      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:java   os   文件   io   问题   cti   

package com.bgsnewlook.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.InputStream;

import java.net.URLEncoder;


public class Download extends ActionSupport {
    private String inputPath;
    private String contentType;
    private String downFileName;

    public String getInputPath() {
        return inputPath;
    }

    public void setInputPath(String inputPath) throws Exception{

            this.inputPath = new String(inputPath.getBytes("iso-8859-1"), "utf-8");

    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getDownFileName() {
        return downFileName;
    }

    public InputStream getTargetFile()

    {
        return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
    }

    public void setDownFileName(String downFileName) throws Exception  {
//        this.downFileName = downFileName;

            this.downFileName = new String(downFileName.getBytes("iso-8859-1"),"utf-8");
           this.downFileName= URLEncoder.encode(this.downFileName,"utf-8");


}
}

struts配置文件struts.xml

  <action name="download" class="com.bgsnewlook.action.Download" >
            <result type="stream" name="success">
                <param name="contentType">${contentType}</param>

                <param name="inputName">targetFile</param>
                <param name="contentDisposition">
                    filename=${downFileName}
                </param>

            </result>

 

 

---down.jsp

<%@ page contentType="text/html;charset=utf-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <title></title>
</head>
<body>
  <a href="download?inputPath=/uploadfiles/1.jpg&contentType=image/jpeg&downFileName=中文">下载文件</a>

</body>
</html>

解决struts2文件下载中文名问题,布布扣,bubuko.com

解决struts2文件下载中文名问题

标签:java   os   文件   io   问题   cti   

原文地址:http://my.oschina.net/u/1248940/blog/289313

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!