以下代码在 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. 算法笔记_222:串中取3个不重复字母(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 从标准输入读入一个由字母构成的串(不大于30个字符). 从该串中取出3个不重复的字符,求所有的取法. 取出的字符,要求按字母升序排列成一个串. 不同 ...

  2. 算法笔记_205:第五届蓝桥杯软件类决赛真题(C语言B组)

    目录 1 年龄巧合 2 出栈次序 3 信号匹配 4 生物芯片 5 Log大侠 6 殖民地   前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 年龄巧合 小明和他的表弟一起去看电影,有人问他们的年龄. ...

  3. 用 bottle.py 写了个简单的升级包上传

    可以当作一个 demo 来玩吧,在这里分享一下.里面涉及的内容包含了文件上传,cookie 设置和读取,重定向(redirect). from bottle import run, post, get ...

  4. Nginx server之Nginx添加ssl支持

    //环境介绍 1.nginx服务器:10.10.54.157 2.配置nginx服务器,当监听到来自客户端www.zijian.com:80请求时,转到10.10.54.150:1500这个web服务 ...

  5. VIM自定义快捷键 abort

    "在选择模式下系统级复制 vmap ,c "+y<ESC>vmap ,C "+Y<ESC>"在选择模式下系统级剪切vmap ,x x:l ...

  6. FTP在CentOS上安装与使用

    安装: yum install -y vsftpd 相关配置文件: /etc/vsftpd/vsftpd.conf //主配置文件,核心配置文件 /etc/vsftpd/ftpusers //黑名单, ...

  7. 写了一个简单的Linux Shell用来下载文件

    #!/bin/sh ; i<; i=i+ )); do # 利用spider来探测请求的资源是否存在,并把请求的结果写入到一个文件 wget --spider --http-user=usern ...

  8. linux常用命令全拼

    命令缩写: pwd:print work directory 打印当前目录 显示出当前工作目录的绝对路径 ps: process status(进程状态,类似于windows的任务管理器) 常用参数: ...

  9. Struts2+JSON数据

    待整理 参考 http://yshjava.iteye.com/blog/1333104 http://kingxss.iteye.com/blog/1622455 JSON中,java.lang.N ...

  10. oracle初始化化表空间用户权限

    oracle单实例安装完毕,需要初始化表空间.用户.等信息.不积跬步,何以至千里! 用sys账户登录oracle数据库,以此来完成所有操作! 01.创建临时表空间 create temporary t ...