最近项目需要在浏览器中通过url预览图片。但发现浏览器始终默认下载,而不是预览。研究了一下,发现了问题:
// 设置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.getb