代码
//参数是需要下载的文件路径
@GetMapping("/download")
public ResponseEntity<byte[]> documentList(@RequestParam String documentAccessory) throws IOException {
byte[] data = Files.readAllBytes(Paths.get(documentAccessory));
String fillName = documentAccessory.substring(documentAccessory.lastIndexOf("/") + 1);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentLength(data.length);
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fillName, "UTF-8"));
return new ResponseEntity<>(data, headers, HttpStatus.OK);
}
postman测试
浏览器测试
这辈子坚持与不坚持都不可怕,怕的是独自走在坚持的道路上!!!