文件在项目Web下
@RequestMapping("/manual.do")
public ResponseEntity<byte[]> companyOperationManual(HttpServletRequest request) throws IOException {
//获取相对路径
String realPath = request.getServletContext().getRealPath("/template/companyOperationManual.pdf");
FileInputStream inputStream = new FileInputStream(realPath);
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
HttpHeaders headers = new HttpHeaders();
String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
headers.setContentDispositionFormData("attachment", URLEncoder.encode(fileName,"utf-8"));
ResponseEntity<byte[]> entity = new ResponseEntity<>(buffer, headers, HttpStatus.OK);
return entity;
}
@GetMapping("/down")
public ResponseEntity<byte[]> down(HttpServletRequest request) throws IOException {
//获取相对路径
ClassPathResource classPathResource = new ClassPathResource(downloadImportStoreTemplatePath);//downloadImportStoreTemplatePath 路径
InputStream inputStream = classPathResource.getInputStream();
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
HttpHeaders headers = new HttpHeaders();
String fileName = downloadImportStoreTemplateName;//downloadImportStoreTemplateName 文件名
headers.setContentDispositionFormData(CommonConstant.ATTACHMENT_FILENAME, URLEncoder.encode(fileName,"utf-8"));
ResponseEntity<byte[]> entity = new ResponseEntity<>(buffer, headers, HttpStatus.OK);
return entity;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/192837.html