以下代码在 chrome、firefox,安卓自带手机浏览器上测试通过,但未经过完全测试,先记录下

    public static void downLoadFile(HttpServletRequest request,HttpServletResponse response,String fullPath,String fileName) throws IOException {
OutputStream outp = response.getOutputStream();
File file = new File(fullPath);
if (file.exists()) {
response.setContentType("APPLICATION/OCTET-STREAM");
       //response.setContentType("application/octet-stream; charset=utf-8");
String filedisplay = fileName;
String agent = (String)request.getHeader("USER-AGENT"); if(agent != null && ( agent.indexOf("MSIE") != -1 || agent.indexOf("Trident") != -1 || agent.indexOf("Mobile") != -1 )) {
//移动浏览器 或 ie Trident是标识是ie浏览器 特别处理ie11 的问题
filedisplay=URLEncoder.encode(filedisplay,"utf-8");
System.out.println("下载文件,移动设备浏览器 或 ie[" + filedisplay + "]");
response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
} else {
         //其他浏览器
String enableFileName = "=?UTF-8?B?" + (new String(Base64.getBase64(filedisplay))) + "?=";
System.out.println("下载文件,其他浏览器[" + enableFileName + "]");
response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
} FileInputStream in = null;
try {
outp = response.getOutputStream();
in = new FileInputStream(fullPath);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
response.flushBuffer();
}
}
} else {
outp.write("文件不存在!".getBytes("utf-8"));
       outp.close();
}
}
    /**
* 获取下载文件的content-type类型。
*
* @param extName 文件后缀
* @return
*/
private String getContextType(String extName, boolean isRead) {
String contentType = "application/octet-stream";
if ("jpg".equalsIgnoreCase(extName) || "jpeg".equalsIgnoreCase(extName)) {
contentType = "image/jpeg";
} else if ("png".equalsIgnoreCase(extName)) {
contentType = "image/png";
} else if ("gif".equalsIgnoreCase(extName)) {
contentType = "image/gif";
} else if ("doc".equalsIgnoreCase(extName) || "docx".equalsIgnoreCase(extName)) {
contentType = "application/msword";
} else if ("xls".equalsIgnoreCase(extName) || "xlsx".equalsIgnoreCase(extName)) {
contentType = "application/vnd.ms-excel";
} else if ("ppt".equalsIgnoreCase(extName) || "pptx".equalsIgnoreCase(extName)) {
contentType = "application/ms-powerpoint";
} else if ("rtf".equalsIgnoreCase(extName)) {
contentType = "application/rtf";
} else if ("htm".equalsIgnoreCase(extName) || "html".equalsIgnoreCase(extName)) {
contentType = "text/html";
} else if ("swf".equalsIgnoreCase(extName)) {
contentType = "application/x-shockwave-flash";
} else if ("bmp".equalsIgnoreCase(extName)) {
contentType = "image/bmp";
} else if ("mp4".equalsIgnoreCase(extName)) {
contentType = "video/mp4";
} else if ("wmv".equalsIgnoreCase(extName)) {
contentType = "video/x-ms-wmv";
} else if ("wm".equalsIgnoreCase(extName)) {
contentType = "video/x-ms-wm";
} else if ("rv".equalsIgnoreCase(extName)) {
contentType = "video/vnd.rn-realvideo";
} else if ("mp3".equalsIgnoreCase(extName)) {
contentType = "audio/mp3";
} else if ("wma".equalsIgnoreCase(extName)) {
contentType = "audio/x-ms-wma";
} else if ("wav".equalsIgnoreCase(extName)) {
contentType = "audio/wav";
}
if ("pdf".equalsIgnoreCase(extName) && isRead)// txt不下载文件,读取文件内容
{
contentType = "application/pdf";
}
if (("sql".equalsIgnoreCase(extName) || "txt".equalsIgnoreCase(extName)) && isRead)// pdf不下载文件,读取文件内容
{
contentType = "text/plain";
}
return contentType;
}

其他参考,但未经验证

http 下载文件时,中文文件名在firefox下乱码的问题,一般在http header中是这样操作的:
"Content-Disposition","attachment;filename=文件名.xx"
其实,按照 rfc231 , Content-Disposition 应该按照如下格式设置:
"Content-Disposition","attachment;filename*=utf-8'zh_cn'文件名.xx"
只要严格按照标准设置以后,自然在各种浏览器下都会正常运行了.
String userAgent = request.getHeader("User-Agent");
String rtn = "";
try {
String new_filename = URLEncoder.encode(fileName, "UTF8");
// 如果没有UA,则默认使用IE的方式进行编码,因为毕竟IE还是占多数的
rtn = "filename=\"" + new_filename + "\"";
if (userAgent != null) {
userAgent = userAgent.toLowerCase();
// IE浏览器,只能采用URLEncoder编码
if (userAgent.indexOf("msie") != -1) {
rtn = "filename=\"" + new_filename + "\"";
}
// Opera浏览器只能采用filename*
else if (userAgent.indexOf("opera") != -1) {
rtn = "filename*=UTF-8''" + new_filename;
}
// Safari浏览器,只能采用ISO编码的中文输出
else if (userAgent.indexOf("safari") != -1) {
rtn = "filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO8859-1") + "\"";
}
// Chrome浏览器,只能采用MimeUtility编码或ISO编码的中文输出
else if (userAgent.indexOf("applewebkit") != -1) {
new_filename = MimeUtility.encodeText(fileName, "UTF8", "B");
rtn = "filename=\"" + new_filename + "\"";
}
// FireFox浏览器,可以使用MimeUtility或filename*或ISO编码的中文输出
else if (userAgent.indexOf("mozilla") != -1) {
rtn = "filename*=UTF-8''" + new_filename;
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return rtn;

JavaWeb下载文件response的更多相关文章

  1. javaweb下载文件模板

    import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax ...

  2. java web 下载文件 response.setHeader()的用法 (转载)

    response.setHeader()的用法 response.setHeader()下载中文文件名乱码问题 收藏 1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但 ...

  3. javaweb下载文件

    //读取文件->写出文件 public static void main(String[] args) { InputStream in =null; OutputStream out = nu ...

  4. 解决javaWEB 下载文件中文名称乱码问题

    response.setContentType("application/x-msdownload;"); response.setCharacterEncoding(" ...

  5. ajax Ajax处理下载文件response没有反应

    参考:https://blog.csdn.net/wf632856695/article/details/52040034

  6. javaweb 之 文件上传与下载

    1.文件上传的原理分析 1.1文件上传的必要前提: a.提供form表单,method必须是post b.form表单的enctype必须是multipart/form-data c.提供input ...

  7. ASP.NET MVC 以Stream 下载文件

     1.0以Stream 下载文件 nl.fileid = Int32.Parse(id); //服务器上对应的id Stream stream = Lawsuit.DownLoad(nl);//服务器 ...

  8. springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)

    参与该项目的文件上传和下载.一旦struts2下完成,今天springmvc再来一遍.发现springmvc特别好包,基本上不具备的几行代码即可完成,下面的代码贴: FileUpAndDown.jsp ...

  9. WebApi和Andriod对接上传和下载文件

    我在实现webapi和Andriod客户端上传下载文件的时候默认的是以流的形式返回的,下面我就贴出最近在研究的对接文件的上传和下载代码以供各位大侠们参考: 上传文件接口: [HttpPost] pub ...

随机推荐

  1. 牛客网-《剑指offer》-包含min函数的栈

    题目:http://www.nowcoder.com/practice/4c776177d2c04c2494f2555c9fcc1e49 辅助栈 C++ class Solution { public ...

  2. VB.NET版+三层实现登陆

    三层已经学了一些时间了,開始认为自己能够用C#敲代码了,就用C#写了一个实现登陆的,真正再用在机房中.还是认为非常吃力的,所以.决定用vb.net敲了.以下是我用vb.net实现的登陆.能够给大家做一 ...

  3. html中文显示乱码的处理方法

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 1. ht ...

  4. infobright系列二:数据迁移

    安装之后把之前infobright的数据迁移到新安装的infobright上. 1:挺掉相关的服务 2:scp 把旧数据拷到新安装的infobright上 3:修改/etc/my-ib.cnf的数据目 ...

  5. 【Zookeeper】源码分析之请求处理链(三)之SyncRequestProcessor

    一.前言 在分析了PrepRequestProcessor处理器后,接着来分析SyncRequestProcessor,该处理器将请求存入磁盘,其将请求批量的存入磁盘以提高效率,请求在写入磁盘之前是不 ...

  6. 字符串问题简述与两个基本问题的Java实现——判断二叉树拓扑结构关系与变形词

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6851631.html  (解题金句:其他问题字符串化,然后调用String类封装方法解决问题: 字符串问题数组 ...

  7. 使用dd命令制作U盘启动盘wodim刻录光盘cd dvd

    首先格式化U盘:使用fdisk -l 查看U盘到挂载点,如我的为/dev/sdb1.卸载U盘,执行格式化命令:mkfs.vfat /dev/sdb1 然后重新挂载U盘,开始制作启动盘: 1.# dd  ...

  8. Orabbix监控Oracle 11g

    Orabbix简介说明 orabbix是一个用来监控oracle数据库性能的zabbix插件工具,通过安装在被监控服务器上客户端上收集数据并传给zabbix服务器端,然后通过调用图形显示.具有以下功能 ...

  9. Redis从入门到精通:初级篇(转)

    原文链接:http://www.cnblogs.com/xrq730/p/8890896.html,转载请注明出处,谢谢 Redis从入门到精通:初级篇 平时陆陆续续看了不少Redis的文章了,工作中 ...

  10. 《web与移动开发》征文活动

    活动简介: 首次征文活动有PHPChina主办,是面向全国所有的IT界活动,希望大家大家以PHPChina论坛为载体,积极的分享技术.希望技术的分享为大家的工作猪跑,也希望大家早日成为技术行业的中流砥 ...