JavaWeb下载文件response
以下代码在 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的更多相关文章
- javaweb下载文件模板
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax ...
- java web 下载文件 response.setHeader()的用法 (转载)
response.setHeader()的用法 response.setHeader()下载中文文件名乱码问题 收藏 1. HTTP消息头 (1)通用信息头 即能用于请求消息中,也能用于响应信息中,但 ...
- javaweb下载文件
//读取文件->写出文件 public static void main(String[] args) { InputStream in =null; OutputStream out = nu ...
- 解决javaWEB 下载文件中文名称乱码问题
response.setContentType("application/x-msdownload;"); response.setCharacterEncoding(" ...
- ajax Ajax处理下载文件response没有反应
参考:https://blog.csdn.net/wf632856695/article/details/52040034
- javaweb 之 文件上传与下载
1.文件上传的原理分析 1.1文件上传的必要前提: a.提供form表单,method必须是post b.form表单的enctype必须是multipart/form-data c.提供input ...
- ASP.NET MVC 以Stream 下载文件
1.0以Stream 下载文件 nl.fileid = Int32.Parse(id); //服务器上对应的id Stream stream = Lawsuit.DownLoad(nl);//服务器 ...
- springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)
参与该项目的文件上传和下载.一旦struts2下完成,今天springmvc再来一遍.发现springmvc特别好包,基本上不具备的几行代码即可完成,下面的代码贴: FileUpAndDown.jsp ...
- WebApi和Andriod对接上传和下载文件
我在实现webapi和Andriod客户端上传下载文件的时候默认的是以流的形式返回的,下面我就贴出最近在研究的对接文件的上传和下载代码以供各位大侠们参考: 上传文件接口: [HttpPost] pub ...
随机推荐
- SSH协议
SSH是一种协议,实现计算机之间的加密登录,即使被截获,截获的也只是加密后的密文,不会泄密. 如果每次登录另外一台计算机,都需要输入密码,就显得太麻烦,所以SSH协议实现了无密码登录,即公钥登录.所谓 ...
- 【Zookeeper】源码分析之持久化(三)之FileTxnSnapLog
一.前言 前面分析了FileSnap,接着继续分析FileTxnSnapLog源码,其封装了TxnLog和SnapShot,其在持久化过程中是一个帮助类. 二.FileTxnSnapLog源码分析 2 ...
- Android——复制项目出现Application Installation Failed
今天把一个项目复制到另一个文件夹下,重新打开结果不能正常安装到虚拟机上了. 解决方法: Build——Clean Project :清除一下项目缓存 Build——Rebuild Project ...
- ReactNative调用aar文件(附:如何打开、查看aar文件内容)
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7275897.html ReactNative可以用来进行一些嵌入式设备的操作终端开发,比如:ATM机.自动售卖 ...
- 有向图的强连通算法 -- tarjan算法
(绘图什么真辛苦) 强连通分量: 在有向图 G 中.若两个顶点相互可达,则称两个顶点强连通(strongly connected). 假设有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有 ...
- 利用Linux文件系统内存cache来提高性能
https://www.linuxjournal.com/article/6345 利用Linux文件系统内存cache来提高性能 本地磁盘文件->socket发送,4步骤数据流向: hard ...
- CentOS7 下 配置Docker远程访问 与 windows下使用maven构筑Spring Boot 的 Docker镜像到远程服务端
1.设置Docker服务端,以支持远程访问: 修改docker服务端配置文件,命令: vim /usr/lib/systemd/system/docker.service 修改后: [Unit] De ...
- 远程过程调用协议(RPC)
RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的 ...
- 友盟消息push功能
友盟地址:https://i.umeng.com/user/products 一.android 1.产品->U-App/U-push->立即使用->管理->左侧-集成测试-& ...
- centos6.5关闭ipv6
万境归空,道法自然 1.在/etc/modprobe.d/目录下增加一个新的配置文件ipv6.conf cat << EOF > /etc/modprobe.d/ipv6.confa ...