@RequestMapping(method = RequestMethod.GET, value = "/download")
@Accessable(name = "附件信息", function = "user", requireRole = false, requireLogin = false)
@ApiOperation(value = "保存附件信息")
@CrossOrigin
void projectDownload(Long id, HttpServletRequest request, HttpServletResponse response) {
JsonResult result = this.service.projectDownload(id);
//绝对路径
String filePath = (String) result.get("absolutePath");
File downloadFile = new File(filePath);
ServletContext context = request.getServletContext();
// get MIME type of the file
String mimeType = context.getMimeType(filePath);
if (mimeType == null) {
mimeType = "application/octet-stream";
log.info("context getMimeType is null");
}
log.info("MIME type: " + mimeType);
// set content attributes for the response
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
// set headers for the response
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
downloadFile.getName());
response.setHeader(headerKey, headerValue);
// Copy the stream to the response's output stream.
try {
InputStream myStream = new FileInputStream(filePath);
IOUtils.copy(myStream, response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
}
}
根据路径--下载文件
最新推荐文章于 2022-10-20 15:55:40 发布
关键词由CSDN通过智能技术生成