文件下载直接返回流即可,
但是如果返回图片pdf等,不想下载只想预览下,
即可以设置不同的头,返回不同的预览流。
/**
* 文件预览
*
* @param bucketName 桶名称
* @param request 请求
* @param response 请求响应
*/
public static void downloadFile(String bucketName, String originalName,
HttpServletRequest request,
HttpServletResponse response) {
try {
InputStream file = getObject (bucketName, originalName);
//文件名乱码处理
String useragent = request.getHeader ("USER-AGENT").toLowerCase ();
if (useragent.contains ("msie") || useragent.contains ("like gecko") || useragent.contains ("trident")) {
originalName = URLEncoder.encode (originalName, StandardCharsets.UTF_8.displayName ());
} else {
originalName = new String (originalName.getBytes (StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
response.setCharacterEncoding ("UTF-8");
switchSetContentType (response, originalName);
// response.setHeader ("Content-Disposition", "attachment;filename=" + originalName);
ServletOutputStream servletOutputStream = response.getOutputStream ();
int len;
byte[] buffer = new byte[1024];
while ((len = file.read (buffer)) > 0) {
servletOutputStream.write (buffer, 0, len);
}
servletOutputStream.flush ();
file.close ();
servletOutputStream.close ();
} catch (Exception e) {
e.printStackTrace ();
log.error (String.format ("下载文件:%s异常 ", originalName));
}
}
/**
* 根据后缀名 来分辨需要返回给页面展示 而非下载 二进制流
* 不设置caontentType 默认就是二进制流 application/octet-stream
* 二进制流的返回 页面回直接下载而非展示
*
* @param response
* @param originalName
*/
private static void switchSetContentType(HttpServletResponse response, String originalName) {
if (!StringUtils.isEmpty (originalName)) {
if (originalName.endsWith (".png")) {
response.setContentType ("image/png");
}
if (originalName.endsWith (".jpg")) {
response.setContentType ("image/jpeg");
}
if (originalName.endsWith (".pdf")) {
response.setContentType ("application/pdf");
}
if (originalName.endsWith (".doc")) {
response.setContentType ("application/msword");
}
if (originalName.endsWith (".xls")) {
response.setContentType ("application/vnd.ms-excel");
}
//-、、、、、可以根据后缀名设置不通的返回类型
}
}
其中getObject()方法不需要理会,你在你本地随便找个文件new 一个输入流,
继续下边的代码就可以。
除了常见的pdf,jpg,img,png等格式,你还可以返回其他的预览格式,
但最终显示与否,需要看你的浏览器是否支持该格式,你使用的前端插件是否可以展示这块。
文件扩展名 |
Content-Type(Mime-Type) |
文件扩展名 |
Content-Type(Mime-Type) |
.*( 二进制流,不知道下载文件类型) |
application/octet-stream |
.tif |
image/tiff |
0.001 |
application/x-001 |
0.301 |
application/x-301 |
0.323 |
text/h323 |
0.906 |
application/x-906 |
0.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 |
application/x-drw |
.dtd |
text/xml |
.dwf |
Model/vnd.dwf |
.dwf |
application/x-dwf |
.dwg |
application/x-dwg |
.dxb |
application/x-dxb |
.dxf |
application/x-dxf |
.edn |
application/vnd.adobe.edn |
.emf |
application/x-emf |
.eml |
message/rfc822 |
.ent |
text/xml |
.epi |
application/x-epi |
.eps |
application/x-ps |
.eps |
application/postscript |
.etd |
application/x-ebx |
.exe |
application/x-msdownload |
.fax |
image/fax |
.fdf |
application/vnd.fdf |
.fif |
application/fractals |
.fo |
text/xml |
.frm |
application/x-frm |
.g4 |
application/x-g4 |
.gbr |
application/x-gbr |
. |
application/x- |
.gif |
image/gif |
.gl2 |
application/x-gl2 |
.gp4 |
application/x-gp4 |
.hgl |
application/x-hgl |
.hmr |
application/x-hmr |
.hpg |
application/x-hpgl |
.hpl |
application/x-hpl |
.hqx |
application/mac-binhex40 |
.hrf |
application/x-hrf |
.hta |
application/hta |
.htc |
text/x-component |
.htm |
text/html |
.html |
text/html |
.htt |
text/webviewhtml |
.htx |
text/html |
.icb |
application/x-icb |
.ico |
image/x-icon |
.ico |
application/x-ico |
.iff |
application/x-iff |
.ig4 |
application/x-g4 |
.igs |
application/x-igs |
.iii |
application/x-iphone |
.img |
application/x-img |
.ins |
application/x-internet-signup |
.isp |
application/x-internet-signup |
.IVF |
video/x-ivf |
.java |
java/* |
.jfif |
image/jpeg |
.jpe |
image/jpeg |
.jpe |
application/x-jpe |
.jpeg |
image/jpeg |
.jpg |
image/jpeg |
.jpg |
application/x-jpg |
.js |
application/x-javascript |
.jsp |
text/html |
.la1 |
audio/x-liquid-file |
.lar |
application/x-laplayer-reg |
.latex |
application/x-latex |
.lavs |
audio/x-liquid-secure |
.lbm |
application/x-lbm |
.lmsff |
audio/x-la-lms |
.ls |
application/x-javascript |
.ltr |
application/x-ltr |
.m1v |
video/x-mpeg |
.m2v |
video/x-mpeg |
.m3u |
audio/mpegurl |
.m4e |
video/mpeg4 |
.mac |
application/x-mac |
.man |
application/x-troff-man |
.math |
text/xml |
.mdb |
application/msaccess |
.mdb |
application/x-mdb |
.mfp |
application/x-shockwave-flash |
.mht |
message/rfc822 |
.mhtml |
message/rfc822 |
.mi |
application/x-mi |
.mid |
audio/mid |
.midi |
audio/mid |
.mil |
application/x-mil |
.mml |
text/xml |
.mnd |
audio/x-musicnet-download |
.mns |
audio/x-musicnet-stream |
.mocha |
application/x-javascript |
.movie |
video/x-sgi-movie |
.mp1 |
audio/mp1 |
.mp2 |
audio/mp2 |
.mp2v |
video/mpeg |
.mp3 |
audio/mp3 |
.mp4 |
video/mpeg4 |
.mpa |
video/x-mpg |
.mpd |
application/vnd.ms-project |
.mpe |
video/x-mpeg |
.mpeg |
video/mpg |
.mpg |
video/mpg |
.mpga |
audio/rn-mpeg |
.mpp |
application/vnd.ms-project |
.mps |
video/x-mpeg |
.mpt |
application/vnd.ms-project |
.mpv |
video/mpg |
.mpv2 |
video/mpeg |
.mpw |
application/vnd.ms-project |
.mpx |
application/vnd.ms-project |
.mtx |
text/xml |
.mxp |
application/x-mmxp |
.net |
image/pnetvue |
.nrf |
application/x-nrf |
.nws |
message/rfc822 |
.odc |
text/x-ms-odc |
.out |
application/x-out |
.p10 |
application/pkcs10 |
.p12 |
application/x-pkcs12 |
.p7b |
application/x-pkcs7-certificates |
.p7c |
application/pkcs7-mime |
.p7m |
application/pkcs7-mime |
.p7r |
application/x-pkcs7-certreqresp |
.p7s |
application/pkcs7-signature |
.pc5 |
application/x-pc5 |
.pci |
application/x-pci |
.pcl |
application/x-pcl |
.pcx |
application/x-pcx |
.pdf |
application/pdf |
.pdf |
application/pdf |
.pdx |
application/vnd.adobe.pdx |
.pfx |
application/x-pkcs12 |
.pgl |
application/x-pgl |
.pic |
application/x-pic |
.pko |
application/vnd.ms-pki.pko |
.pl |
application/x-perl |
.plg |
text/html |
.pls |
audio/scpls |
.plt |
application/x-plt |
.png |
image/png |
.png |
application/x-png |
.pot |
application/vnd.ms-powerpoint |
.ppa |
application/vnd.ms-powerpoint |
.ppm |
application/x-ppm |
.pps |
application/vnd.ms-powerpoint |
.ppt |
application/vnd.ms-powerpoint |