建设银行网站201308,网站建设的基本流程和步骤,小程序直播平台,安徽省交通运输厅网站最近项目需要在浏览器中通过url预览图片。但发现浏览器始终默认下载#xff0c;而不是预览。研究了一下#xff0c;发现了问题#xff1a;// 设置response的header#xff0c;注意这句#xff0c;如果开启#xff0c;默认浏览器会进行下载操作#xff0c;如果注释掉而不是预览。研究了一下发现了问题// 设置response的header注意这句如果开启默认浏览器会进行下载操作如果注释掉浏览器会默认预览。 response.addheader(content-disposition, attachment;filename fileutil.getoriginalfilename(path));然后需要注意response.setcontenttype(contenttype);//不同的文件类型contenttype不一样比如图片一般是image/jpeg、image/png等requestmapping(value getfile/{folder}/{filename:.}*, method requestmethod.get)public void getfile(httpservletresponse response, pathvariable string folder,pathvariable string filename){// 设置编码response.setcharacterencoding(utf-8);try{string path folder / filename;boolean flag ossclient.doesobjectexist(ossproperties.getbucket(), path);// 判断文件是否存在if (flag){// 清空responseresponse.reset();// 设置response的header注意这句如果开启默认浏览器会进行下载操作如果注释掉浏览器会默认预览。// response.addheader(content-disposition,// attachment;filename fileutil.getoriginalfilename(path));// response.addheader(content-length, buf.length);outputstream toclient new bufferedoutputstream(response.getoutputstream());// bytearrayoutputstream bos new bytearrayoutputstream(1024);ossobject ossobject ossclient.getobject(ossproperties.getbucket(), path);string contenttype ossobject.getobjectmetadata().getcontenttype();system.out.println(contenttype);//注意contenttype类型response.setcontenttype(contenttype);byte[] buf new byte[1024];inputstream in ossobject.getobjectcontent();int l;while ((l in.read(buf)) ! -1){// if (buf.length ! 0)// {toclient.write(buf, 0, l);// }}in.close();// 写完以后关闭文件流toclient.flush();toclient.close();// response.getoutputstream().write(bos.tobytearray());}else{response.senderror(httpservletresponse.sc_not_found, 找不到相关资源);}}catch (ioexception e){e.printstacktrace();}}补充知识【java文件下载】如何让浏览器直接下载后端返回的图片而不是直接打开默认情况下浏览器设定是inline形式对于服务器返回的文件能打开就打开不能打开就自动下载。content-disposition 设置大多数情况下后端都是实现一个文件管理的功能通过文件的唯一标志去获取文件流。后端都会读取文件然后文件的流写入到response的输出流这样就可以实现文件的访问了。但是有些时候实现下载功能后端返回的是图片浏览器却直接把图片打开了怎么回事这就是content-disposition设置的问题如下都是java示例设置为inline,如果浏览器支持该文件类型的预览就会打开而不是下载response.setheader(content-disposition, inline; filename111.jpg);设置为attachment,浏览器则直接进行下载纵使他能够预览该类型的文件。response.setheader(content-disposition, attachment; filename111.jpg);特别说明chrome不设置content-type也会自动打开如果是它可识别预览的文件。示例代码package cn.hanquan.controller;import java.io.file;import java.io.ioexception;import javax.servlet.servletoutputstream;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import org.apache.commons.io.fileutils;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.servlet.modelandview;controllerpublic class demodownload {requestmapping(download)public void download(string filename, httpservletresponse res, httpservletrequest req) throws ioexception {// 设置响应流中文件进行下载// attachment是以附件的形式下载inline是浏览器打开// bbb.txt是下载时显示的文件名// res.setheader(content-disposition, attachment;filenamebbb.txt); // 下载res.setheader(content-disposition, inline;filenamebbb.txt); // 浏览器打开// 把二进制流放入到响应体中servletoutputstream os res.getoutputstream();system.out.println(here download);string path req.getservletcontext().getrealpath(files);system.out.println(path is: path);system.out.println(filename is: filename);file file new file(path, filename);byte[] bytes fileutils.readfiletobytearray(file);os.write(bytes);os.flush();os.close();}}浏览器直接打开效果下载效果以上这篇java读取文件流,设置浏览器下载或直接预览操作就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持萬仟网。希望与广大网友互动点此进行留言吧