inline 對於壹些本地裝有相應可以打開的軟件,並且關聯的,瀏覽器會嘗試直接打開查看,不提示下載,attachment則會提示下載,當然也會因不同的瀏覽器和不同設置而不同
java web中下載文件時,我們壹般設置Content-Disposition告訴瀏覽器下載文件的名稱,是否在瀏覽器中內嵌顯示.
Content-disposition: inline; filename=foobar.pdf
表示瀏覽器內嵌顯示壹個文件
Content-disposition: attachment; filename=foobar.pdf
表示會下載文件,如火狐瀏覽器中
spring mvc中
Java代碼 收藏代碼
@ResponseBody
@RequestMapping(value = "/download",produces="application/octet-stream")
public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2)
throws IOException {
byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg");
response.addHeader("Content-Disposition", "inline;filename=\"a.jpg\"");
return bytes;
}
如上代碼中是內嵌顯示圖片呢?還是會彈框下載呢?
答案是:彈框下載
為什麽呢?設置為inline應該是內嵌顯示啊!
因為response content type設置成了"application/octet-stream"
註意:我們說是內嵌顯示還是下載,那壹定是針對可內嵌顯示的類型,例如"image/jpeg","image/png"等.
看下面的例子:設置response content type為"image/jpeg"
Java代碼 收藏代碼
@ResponseBody
@RequestMapping(value = "/download",produces="image/jpeg")
public byte[] downloadFile(HttpServletRequest request, HttpServletResponse response,String contentType2,String downloadType)
throws IOException {
byte[]bytes=FileUtils.getBytes4File("D:\\Temp\\cc.jpg");
response.addHeader("Content-Disposition", downloadType+";filename=\"a.jpg\"");
return bytes;
}
在瀏覽器中訪問:http://localhost:8080/tv_mobile/video/download?downloadType=inline 時就內嵌顯示: