content-disposition使用

content-disposition 是RFC中定义的文件下载标识字段,详情查看rfc2616章节19.5 Additional Features,其中有两种形式供给我们选择,一个是inline,一个是attachment

在页面内打开代码:

1
2
3
4
5
File file = new File("rfc1806.txt");  
String filename = file.getName();
response.setHeader("Content-Type","text/plain");
response.addHeader("Content-Disposition","inline;filename=" + new String(filename.getBytes(),"utf-8"));
response.addHeader("Content-Length","" + file.length());

弹出保存框代码:

1
2
3
4
5
File file = new File("rfc1806.txt");  
String filename = file.getName();
response.setHeader("Content-Type","text/plain");
response.addHeader("Content-Disposition","attachment;filename=" + new String(filename.getBytes(),"utf-8"));
response.addHeader("Content-Length","" + file.length());

场景应用,导出word文档:

控制层使用的springmvc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 导出word功能
* @param workflowId 流程的id
* @throws IOException
*/
@ApiOperation(value="导出word功能",notes="导出word功能")
@ApiResponses(value= {@ApiResponse(code=200,message="导出成功",response=BimWorkflowDetailController.class)})
@RequestMapping(value="/exportCollision")
public String exportCollision(String workflowId,HttpServletResponse response) throws IOException {
//response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("content-disposition", "attachment;filename=" + new SimpleDateFormat("yyyyMMddHH:mm:ss").format(new Date(System.currentTimeMillis())) + ".doc");
// opera和firefox可以正常使用,而ie不能正常使用 添加下列头
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","No-cache");
response.setDateHeader("Expires",0);

ApiResultEntity apiResultEntity = new ApiResultEntity();
apiResultEntity.setDataEncode(true);
// 1.根据workflowid从数据库查询相关信息
Parameter parameter = new Parameter(BizServiceDefine.bimWorkflowDetailService, "getBimWorkflowInfoById").setId(NumberUtil.tryParseLong(workflowId));
parameter = bizProvider.execute(parameter);
apiResultEntity = (ApiResultEntity) parameter.getResult();
Map<String,Object> param = (Map<String,Object>) apiResultEntity.getData();
BimWorkflowDetail workflowBase = (BimWorkflowDetail) param.get("bimWorkflowDetail");
List<BimWorkflowCollision> collisionLists = (List<BimWorkflowCollision>) param.get("collisionLists");
OutputStream out = null;
try {
out = response.getOutputStream();
// 存word
saveDoc(workflowBase, collisionLists, out);
} catch (Exception e) {
logger.error("导出word异常!!!!");
e.printStackTrace();
}
out.flush();
out.close();
return null;
}

content-disposition使用
http://example.com/2018/03/12/2018-03-12-java-content-disposition/
Author
Hoey
Posted on
March 12, 2018
Licensed under