@GetMapping(value = "/downloadLabel/{shippingLabelFile}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Resource> downloadFile(@PathVariable("shippingLabelFile") String shippingLabelFile) {
return service.downloadLabel(shippingLabelFile);
}
public ResponseEntity<Resource> downloadLabel(String shippingLabelFile) {
return localFileManager.download(shippingLabelFile);
}
public ResponseEntity<Resource> download( String fileName) {
try {
Resource resource=new UrlResource(
//拼接下载的文件的原路径
Paths.get(filePath +File.separator+fileName).toUri());
if(resource.exists() && resource.isReadable()){
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
.header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\""+
resource.getFilename()+"\"").body(resource);
}
} catch (MalformedURLException e) {
log.debug(e.getMessage());
}
return null;
}
报错
2022-05-10 18:57:25.279 WARN 27532 --- [io-8400-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class org.springframework.core.io.UrlResource] with preset Content-Type 'application/octet-stream']