java文件下载至linux乱码,JAVA 文件下载乱码问题解决办法

页面设置隐藏的iframe

页面下载按钮

id="btnDown" href="javascript:void(download('btnDown'))" >下载模板

页面JS脚本

function download(id){

$("#reqFrame").attr("src",encodeURI("/servlet/Common?action=downloadByPath&filePath="+$("#"+id).attr("title")));

}

后台servlet方法

public class SV_Common extends HttpServlet {

/**

*

*/

private static final long serialVersionUID = 1L;

public static final Logger logger = Logger.getLogger(SV_Common.class

.getName());

protected void service(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

String action = request.getParameter("action");

try {

if ("downloadByPath".equals(action)) { //下载指定路径的文件

downloadByPath(request, response);

} else {

throw new IllegalArgumentException("没有相匹配的操作类型,请检查opp变量.");

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

private void downloadByPath(HttpServletRequest request,

HttpServletResponse response) throws IOException, ServletException {

String filePath = request.getParameter("filePath");

logger.info("filePath="+filePath);

String fileFullName = filePath.substring(filePath.lastIndexOf("/") + 1);

response.reset();

if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {

logger.info("User-Agent=firefox");

response.setHeader(

"Content-Disposition",

"attachment;filename=" + new String(fileFullName.getBytes("UTF-8"), "ISO8859-1")

);

} else {

logger.info("User-Agent=not firefox");

response.setHeader(

"Content-Disposition",

"attachment;filename=" + URLEncoder.encode(fileFullName, "UTF-8")

);

}

File file = new File(PathUtil.getPath("") + "/" + filePath);

response.setContentType(new MimetypesFileTypeMap().getContentType(file));

OutputStream out = response.getOutputStream();

BufferedInputStream in = null;

byte[] buffer = new byte[8192];

int length;

try

{

in = new BufferedInputStream(

new FileInputStream(file),8192

);

while ( (length = in.read(buffer)) != -1)

{

out.write(buffer, 0 ,length);

}

}

catch (IOException ex)

{

ex.printStackTrace();

}

finally

{

if (out != null)

{

try {out.close();}

catch (IOException ex) {}

out = null;

}

if (in != null)

{

try {in.close();}

catch (IOException ex) {}

in = null;

}

}

response.flushBuffer();

};

}

paip.java swt 乱码问题解决

paip.java swt 乱码问题解决 看累挂,Dfile.encoding是gbk的.. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专 ...

ASP+Access UTF-8 网页乱码问题解决办法

用ACCESS数据库和ASP做网站时用UTF-8编码有时会出现乱码,再者网页出错或者刷新页面后就是乱码,如果数据库取值乱码在开头加上

openerp 7.0邮件接收中文附件乱码问题解决办法

openerp 7.0邮件接收中文附件乱码问题解决办法: 修改文件\addons\mail\mail_thread.py #1064 line插入代码: h=email.Header.Header(n ...

关于jFinal开发中遇到的中文乱码问题解决办法

关于jFinal开发中遇到的中文乱码问题解决办法 设置tomcat的编码,修改

node-canvas

1.使用之前需要预先安装  Cairo 本人安装遇到各种各样的坑,可以参考这里来填坑:https://github.com/Automattic/node-canvas/wiki/Installati ...

【转】 SED多行模式空间

1. sed执行模板=sed '模式{命令1;命令2}' 即逐行读入模式空间,执行命令,最后输出打印出来 2. 为方便下面,先说下p和P,p打印当前模式空间内容,追加到默认输出之后,P打印当前模式空间 ...

LSM

1.MySQL存储引擎: B+树 https://blog.csdn.net/qq_26222859/article/details/80631121 2.HBase LSM树 核心:将对数据的修改增 ...

[原]关于helios自定义面板简述

想研究一下helios和自己仿真软件的适配,于是找了一下关于helios的使用 首先关于helios的使用有一个“vr2009”的发帖者有过很详细描述: http://www.insky.cn/bbs ...

HTML Tags

While some tags have a very specific purpose, such as image and video tags, most tags are used to de ...

重整ADO.NET连接池相关资料

https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectionstring(VS.80) ...

ural Ambitious Experiment 树状数组

During several decades, scientists from planet Nibiru are working to create an engine that would all ...

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值