@Api(tags = "文件模板下载")
@RestController
@RequestMapping("fileDownloadHandler")
public class FileDownloadHandler {
private static final String EXECL_FILE_PATH = "template/excel/";
@ApiOperation("文件模板下载")
@PostMapping
public void downloadFileWithOriginalName(HttpServletResponse response, String filename) throws IOException {
LogUtils.info("开始下载");
String path = Objects.requireNonNull(Objects.requireNonNull(ClassUtils.getDefaultClassLoader()).getResource("")).getPath();
File file = new File(path + EXECL_FILE_PATH + filename);
if (!file.exists()) {
throw new DSTABusException("文件不存在");
}
// 设置响应头
response.addHeader("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(file.getName(), "UTF-8"));
response.setContentType("application/octet-stream");
response.setContentLengthLong(file.length());
// 下载文件
try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
FileCopyUtils.copy(inputStream, response.getOutputStream());
}
LogUtils.infoHigh("下载完成");
}
}
文件模板下载
最新推荐文章于 2024-07-10 21:37:49 发布