页面设置隐藏的iframe

<iframe id='reqFrame' frameborder='0'  style='display:none' allowtransparency='true' ></iframe>

页面下载按钮

<a  class="easyui-linkbutton" data-options="iconCls:'icon-ok'"  title="/demo/省本部固定资产明细表.xlsx" 
id="btnDown" href="javascript:void(download('btnDown'))" >下载模板</a>

页面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();
};
}

JAVA 文件下载乱码问题解决办法的更多相关文章

  1. paip.java swt 乱码问题解决

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

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

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

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

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

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

    关于jFinal开发中遇到的中文乱码问题解决办法 设置tomcat的编码,修改 <Connector port="8080" protocol="HTTP/1.1& ...

  5. JAVA文件下载功能问题解决日志

    今天给报告系统做了个下载功能,遇到了挺多问题,通过查资料一一解决了. 1.首先遇到的问题是:java后台的输出流输出之后,没有任何报错,浏览器端不弹出保存文件的对话框,原本是ajax请求到后台的con ...

  6. Servlet中文乱码问题解决办法

    首先对于源jsp网站和servlet里面的字符集要一样,一般支持中文的字符集为UTF-8最好采用这个字符集(除此之外还有gb2312); 对于源jsp文件的代码中需要设置 设置你的page里面的字符集 ...

  7. zabbix图形乱码问题解决办法

    zabbix中的图形乱码的问题解决办法: 1.下载字体,例如:simkai.ttf楷体(注:在windows中的字体格式可能是TTC的,所以去网上下载一个ttf的字体) 2.上传到linux中(我使用 ...

  8. PuTTY乱码问题解决办法

    原文链接:http://www.henshiyong.com/archives/403.html 使用PuTTY 时,遇到了乱码问题,查看了别人介绍的信息,解决掉了. 方法其实很简单,现在分享出来. ...

  9. loadrunner乱码问题解决办法

    7.LoadRunner回放脚本时,在浏览器显示的中文是乱码 最近,遇到了好多乱码的问题,解决了一些,还有最后一个乱码,能想到的各种办法都试过了,还是不行,很奇怪啊. 解决这些乱码时,涉及到了http ...

随机推荐

  1. windows下能读写linux分区的软件 转

    1. ext2ifs 这个工具与explore2fs都是John Newbigin使用Delphi写的,explore2fs Copyright (C) 2000,Ext2IFS v0.3 Copyr ...

  2. UITableView优化的那些事儿

    作为iOS开发,UITableView可能是平时我们打交道最多的UI控件之一,其重要性不言而喻. 关于TableView,我想最核心的就是UITableViewCell的重用机制了. 简单来说呢就是当 ...

  3. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  4. 2、Charm Bracelet( poj 3624)简单0-1背包

    题意:有n件手镯,总重量不能超过M,每个手镯有一个体重W[i]和魅力V[i],问在不超过M的情况下能获得的魅力总和 思路:把M当背包总容量,用0-1背包写 代码: #include <iostr ...

  5. GitCam一款Gif动画制作软件

    本篇文章由:http://www.sollyu.com/gitcam-a-gif-animation-software/ 说明 GifCam是一款小巧.免费的录制电脑屏幕并制作成GIF动画的软件,具有 ...

  6. 使用win8.1 x64 office2010 php 使用 pdo_odbc 连接excel失败的问题

    public function init($filePath){ $dbq = iconv('UTF-8',"GBK",BASEPATH.'../'.$filePath); $ds ...

  7. PHP trim去空格函数

    trim() 能除去的字符有“ ”空格."\t"水平制表符."\n"换行符."\r"回车符."\0字符串结束符".&qu ...

  8. 强大的字符串格式化函数 - format

    自python2.6开始,新增了一种格式化字符串的函数str.format(),它通过{}和:来代替% 位置方法格式化 >>>'{}-{}'.format('simon','ting ...

  9. 计划任务实现定时备份mysql数据库

    1.linux平台 30 3 * * * sh /data/tools/mysqlbackup.sh  每天3点半备份数据库mysqlbackup.sh(备份最近5天的数据): #设置数据库名,数据库 ...

  10. C++的类和对象

    #include <iostream> // 预处理命令 using namespace std; class Student{ // 声明一个类,类名为Student private : ...