其他分享
首页 > 其他分享> > SpringMVC使用ResponseEntity实现文件下载

SpringMVC使用ResponseEntity实现文件下载

作者:互联网

本文主要通过ResponseEntity<byte[]>实现文件下
该类实现响应头、文件数据(以字节存储)、状态封装在一起交给浏览器处理以实现浏览器的文件下载。
ResponseEntity参数解释:ResponseEntity(T body, MultiValueMap<String, String> headers, HttpStatus statusCode)
其中ResponseEntity<T> extends HttpEntity<T>,很明显的继承关系,HttpEntity是一个实体类,在new ResponseEntity<byte[]>(b, headers, statusCode);这句初始化的时候,会将T body, MultiValueMap<String, String> headers两个参数传给父类,本类中存放状态码,在HttpEntity类的源码中可以看到:

public HttpEntity(T body, MultiValueMap<String, String> headers) {
this.body = body;
HttpHeaders tempHeaders = new HttpHeaders();
if (headers != null) {
tempHeaders.putAll(headers);
}
//将header头转变成只能读取的对象,而不是写入的对象。
this.headers = HttpHeaders.readOnlyHttpHeaders(tempHeaders);
}

HttpHeaders类说明:表示HTTP请求和响应头,将字符串头名映射到字符串值的列表。
在这里为什么要用HttpHeaders类,是因为MultiValueMap接口的实现类是:HttpHeaders、LinkedMultiValueMap以及静态类MultiValueMapAdapter
话不多说直接上代码:

//下载练习

@GetMapping("/download")
public ResponseEntity<byte[]> download(@RequestParam(name = "id") Long id, HttpServletRequest request) {
TemplateDto byId = getBaseService().findById(id);
HttpHeaders headers = new HttpHeaders();
String fileName = null;
try {
fileName = FileExtraUtils.handleFileName(request, "download.pdf");
} catch (UnsupportedEncodingException e) {
//ignore
log.warn("文件名处理出错", e);
}
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

return new ResponseEntity<>(byId.getTemplaeContent(), headers, HttpStatus.OK);
}

//上传文件并保存
@PostMapping("/upload")
public void upload(@Param("file") MultipartFile file, HttpServletResponse response) throws IOException {
File tempFile;
try {
tempFile = File.createTempFile(file.getOriginalFilename(), ".pdf");
file.transferTo(tempFile);
HashMap data = new HashMap();
data.put("fileName", tempFile.getCanonicalPath());
data.put("displayName", file.getOriginalFilename());
ResultDto dto = ResultDtoFactory.toAckData(data);
response.setContentType("text/html; charset=utf-8");
response.getWriter().write(JSON.toJSONString(dto));
} catch (IOException e) {
log.error("保存文件失败", e);
ResultDto dto = ResultDtoFactory.toNack("文件上传失败");
response.setContentType("text/html; charset=utf-8");
response.getWriter().write(JSON.toJSONString(dto));
}
}

@Override
public ResultDto create(TemplateDto templateDto) {
String fileName = templateDto.getFileName();
try (FileInputStream fis = new FileInputStream(new File(fileName))) {
templateDto.setTemplaeContent(IOUtils.toByteArray(fis));
} catch (IOException e) {
log.error("读取模版内容出错", e);
return ResultDtoFactory.toNack("读取模板内容出错,请重新上传");
}
getBaseService().save(templateDto);
return ResultDtoFactory.toAck();
}


标签:body,SpringMVC,fileName,headers,ResponseEntity,new,HttpHeaders,下载
来源: https://www.cnblogs.com/renjiaqi/p/11457229.html