admin管理员组文章数量:1130349
背景:从后端获取文件流,现在谷歌浏览器直接预览或下载
涉及后端返回请求头参数:content-disposition、content-type
ps:谷歌支持直接预览的文件类型有pdf、png、mp4、gif等
代码示例:
//js代码
window.open("后端文件下载地址")
/**
* 预览或下载
* @param preview 0为下载,1为预览
* @param fileName
* @param response
* @throws IOException
*/
@RequestMapping("/download/{preview}")
public void downloadAttachment(@PathVariable Integer preview,String fileName, HttpServletResponse response) throws IOException {
try {
//这里是你读取文件流的地方,从mongodb或者其他数据库操作
... ...
//假设存放真实文件名的参数是originalFilename
// 这里URLEncoder.encode可以防止中文乱码
String displayName = URLEncoder.encode(originalFilename, "UTF-8");
//attachment:附件,文件以附件的方式处理
String Content_disposition = "attachment;filename=" + displayName;
//octet-stream:八进制
String content_type = "application/octet-stream";
//预览
if(1 == preview){
//inline:内联,文件以内联的方式处理(即网页或者页面的一部分)
Content_disposition = "inline;filename=" + displayName;
content_type = "application/pdf";
}
response.setContentType(content_type);
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", Content_disposition);
// 将字节流写入response.getOutputStream()对象
... ...
} catch (Exception e) {
log.error(e.getMessage(),e);
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
ServerResponse<String> fail = ServerResponse.fail(e.getMessage());
response.getWriter().println(JSON.toJSONString(fail));
}
}
总结:使用{“content-disposition”: “attachment;filename=1.pdf”, “content-type”: “application/octet-stream”},可以直接下载;使用{“content-disposition”: “inline;filename=1.pdf”, “content-type”: “application/pdf”},可以在页面上进行预览。
背景:从后端获取文件流,现在谷歌浏览器直接预览或下载
涉及后端返回请求头参数:content-disposition、content-type
ps:谷歌支持直接预览的文件类型有pdf、png、mp4、gif等
代码示例:
//js代码
window.open("后端文件下载地址")
/**
* 预览或下载
* @param preview 0为下载,1为预览
* @param fileName
* @param response
* @throws IOException
*/
@RequestMapping("/download/{preview}")
public void downloadAttachment(@PathVariable Integer preview,String fileName, HttpServletResponse response) throws IOException {
try {
//这里是你读取文件流的地方,从mongodb或者其他数据库操作
... ...
//假设存放真实文件名的参数是originalFilename
// 这里URLEncoder.encode可以防止中文乱码
String displayName = URLEncoder.encode(originalFilename, "UTF-8");
//attachment:附件,文件以附件的方式处理
String Content_disposition = "attachment;filename=" + displayName;
//octet-stream:八进制
String content_type = "application/octet-stream";
//预览
if(1 == preview){
//inline:内联,文件以内联的方式处理(即网页或者页面的一部分)
Content_disposition = "inline;filename=" + displayName;
content_type = "application/pdf";
}
response.setContentType(content_type);
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", Content_disposition);
// 将字节流写入response.getOutputStream()对象
... ...
} catch (Exception e) {
log.error(e.getMessage(),e);
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
ServerResponse<String> fail = ServerResponse.fail(e.getMessage());
response.getWriter().println(JSON.toJSONString(fail));
}
}
总结:使用{“content-disposition”: “attachment;filename=1.pdf”, “content-type”: “application/octet-stream”},可以直接下载;使用{“content-disposition”: “inline;filename=1.pdf”, “content-type”: “application/pdf”},可以在页面上进行预览。
版权声明:本文标题:后端返回文件流,如何在谷歌预览与下载 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1758777828a2784078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论