spring boot文件下载
示例代码
@GetMapping("/download")
public ResponseEntity<byte[]> dowloadFile() throws IOException {
File file = new File("文件索引");
ResponseEntity.BodyBuilder builder = ResponseEntity.ok();
builder.contentLength(file.length());
builder.contentType(MediaType.APPLICATION_OCTET_STREAM);
builder.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
return builder.body(FileUtils.readFileToByteArray(file));
}
本文提供了一个使用SpringBoot实现文件下载的示例代码。通过@GetMapping注解定义了/download路由,利用ResponseEntity返回文件数据,设置文件长度、内容类型及下载方式,确保文件正确下载。
1218

被折叠的 条评论
为什么被折叠?



