博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决struts2文件下载中文名问题
阅读量:7095 次
发布时间:2019-06-28

本文共 1659 字,大约阅读时间需要 5 分钟。

hot3.png

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>

转载于:https://my.oschina.net/u/1248940/blog/289313

你可能感兴趣的文章
设计流程及工具记录
查看>>
关于CDialogBar的编程
查看>>
吹きすさぶ风の中で
查看>>
对象引用前加const 报错
查看>>
linux 0.11 源码学习(十一)
查看>>
编码风格——linux内核开发的coding style
查看>>
表格隔行变色案例
查看>>
IOS 模拟不同网络环境 - Network Link Conditioner
查看>>
JAVA第一周学习
查看>>
sql语句select group by order by where一般先后顺序 转载
查看>>
for循环是怎么工作的
查看>>
java版spring cloud+spring boot+redis多租户社交电子商务平台(十二)断路器监控(Hystrix Dashboard)...
查看>>
我将用一个月学会android并开发一个应用,这篇主要用来记录自己的学习历程
查看>>
TX2-入门坑点-Ubuntu16.04任务栏消失
查看>>
Java中内存分配方式
查看>>
MySQL生产库全库备份脚本
查看>>
IOS 的 PUSH 封装
查看>>
支付宝支付
查看>>
第三周 动态规划算法(1):1.集合加法
查看>>
iPhone 上怎么给CSS定义 active 样式
查看>>