以下代码在 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. 为javascript设置默认参数值

    javascript(js)中如何为函数设置默认参数值,下面提供几种方法供参考. 第一种方法: function example(a,b){ var a = arguments[0] ? argume ...

  2. 去除console.log()打印语句

    打印语句:console.log() ,一句话描述它! “用的时候感觉贼爽,不用的时候脑袋痛吧?” 以下提供三种解决方案: 一. webpack打包时去除,适合Vue项目 二. vscode正则匹配, ...

  3. Sqlite和Mysql和SqlServer中insert … select … where not exist的用法

    下面介绍Mysql和Sqlite和Sqlserver中,根据select的条件判断是否插入.例如: 一.Mysql中: INSERT INTO books (name) SELECT 'SongXin ...

  4. 转:nginx模块开发——handler(一)

    handler模块简介 相信大家在看了前一章的模块概述以后,都对nginx的模块有了一个基本的认识.基本上作为第三方开发者最可能开发的就是三种类型的模块,即handler,filter和load-ba ...

  5. HDOJ 5288 OO’s Sequence 水

    预处理出每一个数字的左右两边能够整除它的近期的数的位置 OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 13 ...

  6. 測试AtomicInteger与普通int值在多线程下的递增操作

    日期: 2014年6月10日 作者: 铁锚 Java针对多线程下的数值安全计数器设计了一些类,这些类叫做原子类,当中一部分例如以下: java.util.concurrent.atomic.Atomi ...

  7. vue框架整体流程

    1.整体流程 (1)模板解析成render函数 (2)响应式监听 (3)首次渲染,显示页面,绑定依赖 (4)data属性变化,触发rerender 2.模板解析为render函数 参考上一篇博客. 模 ...

  8. Java线程池的使用以及原理

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6543909.html  一:线程池简介 在使用多线程来提高处理器利用率的同时,由于线程的不断创建和销毁所造成的 ...

  9. vi编辑器的常用命令

    游标控制 h 游标向左移 j 游标向下移 k 游标向上移 l (or spacebar) 游标向右移 w 向前移动一个单词 b 向后移动一个单词 e 向前移动一个单词,且游标指向单词的末尾 ( 移到当 ...

  10. ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)

    [root@ldaptest openldap]# ldapadd -x -D "cn=admin,dc=ultrapower,dc=com" -W -f /tmp/base.ld ...