Java文件下载ContentType设置

String filePath="";

File file=new File(filePath);

//解决乱码问题

String filename=URLEncoder.encode(file.getName(),"utf-8");

//重置输出流

response.reset();

Response.AddHeader("Content-Disposition", "attachment;filename="+filename);//设置文件名

response.addHeader("Content-Length",file.length);//设置下载文件大小

response.setContentType("application/octet-stream");//设置文件类型

OutputStream toClient=new BufferedOuntputStream( response.getOutputStream() );//获取二进制输出流

//读取文件数据

InputStream fis=new BufferedInputStream(new FileInputStream(filePath));

byte[] buffer=new byte[file.length()];

fis.read(buffer);

fis.close();

//输出文件数据

toClient.write(buffer);

toClient.close();

文件类型如下:

".*"="application/octet-stream"

".001"="application/x-001"

".301"="application/x-301"

".323"="text/h323"

".906"="application/x-906"

".907"="drawing/907"

".a11"="application/x-a11"

".acp"="audio/x-mei-aac"

".ai"="application/postscript"

".aif"="audio/aiff"

".aifc"="audio/aiff"

".aiff"="audio/aiff"

".anv"="application/x-anv"

".asa"="text/asa"

".asf"="video/x-ms-asf"

".asp"="text/asp"

".asx"="video/x-ms-asf"

".au"="audio/basic"

".avi"="video/avi"

".awf"="application/vnd.adobe.workflow"

".biz"="text/xml"

".bmp"="application/x-bmp"

".bot"="application/x-bot"

".c4t"="application/x-c4t"

".c90"="application/x-c90"

".cal"="application/x-cals"

".cat"="application/vnd.ms-pki.seccat"

".cdf"="application/x-netcdf"

".cdr"="application/x-cdr"

".cel"="application/x-cel"

".cer"="application/x-x509-ca-cert"

".cg4"="application/x-g4"

".cgm"="application/x-cgm"

".cit"="application/x-cit"

".class"="java/*"

".cml"="text/xml"

".cmp"="application/x-cmp"

".cmx"="application/x-cmx"

".cot"="application/x-cot"

".crl"="application/pkix-crl"

".crt"="application/x-x509-ca-cert"

".csi"="application/x-csi"

".css"="text/css"

".cut"="application/x-cut"

".dbf"="application/x-dbf"

".dbm"="application/x-dbm"

".dbx"="application/x-dbx"

".dcd"="text/xml"

".dcx"="application/x-dcx"

".der"="application/x-x509-ca-cert"

".dgn"="application/x-dgn"

".dib"="application/x-dib"

".dll"="application/x-msdownload"

".doc"="application/msword"

".dot"="application/msword"

".drw"=&#
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值