以下代码在 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. linux测试工程介绍(Linux Test Project)

    http://ltp.sourceforge.net/ Linux Test Project, 后台很硬,由SGI™ 发起, IBM维护,所以质量有保障. 里面介绍了很多工具,对于一般的基准测试应该是 ...

  2. Android总结之WebView与Javascript交互[转]

    Android总结之WebView与Javascript交互   前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.We ...

  3. JS 处理Json数据事例

    JS从远端获取数据之后,往往还需要在处理一下,下面给出一个事例,供参考 将'[{"role_id":1,"enable":1},{"role_id&q ...

  4. C# ListView用法

    ListView是个较为复杂的控件       1.定义   把它拽进来,系统会自动在Designer.cs里添加一个  this.listView1 = new System.Windows.For ...

  5. Git诞生

    很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与 ...

  6. Nginx server之Nginx添加ssl支持

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

  7. gzip:stdin:not in gzip format的解决办法

    执行解压命令,在解压.gz或者.bz2格式的文件的文件的时候可能会出现这样的错误提示 tar -zxvf rlwrap-0.30.tar.gz 报错如下 gzip: stdin: not in gzi ...

  8. 高效编写微信小程序

    原文:https://isux.tencent.com/high-performance-wechat-app-development.html 前言 微信小程序是一个工程,就和盖房子一样,打好了地基 ...

  9. Mac OS使用技巧十九:Safari碉堡功能之二查看网页源代码

         由于大三下的时候选修了搜索技术.了解了网络上搜索引擎和网络爬虫的信息扒取的一些东西,后来我们做了一个比較水的东西.就是仅仅扒取了几家较大的下载站点几十个软件的评分下载量等信息,当用户输入一个 ...

  10. Easyui入门视频教程 第08集---登录实现 ajax button的使用

    目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标自定义   Easyui入门视频教程 第08集---登录实现 ajax button的使用  ...