我正在尝试从Spring Boot Rest服务中下载文件。
@RequestMapping(path="/downloadFile",method=RequestMethod.GET)
@Consumes(MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<InputStreamReader> downloadDocument(
String acquistionId,
String fileType,
Integer expressVfId) throws IOException {
File file2Upload = new File("C:\\Users\\admin\\Desktop\\bkp\\1.rtf");
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
InputStreamReader i = new InputStreamReader(new FileInputStream(file2Upload));
System.out.println("The length of the file is : "+file2Upload.length());
return ResponseEntity.ok().headers(headers).contentLength(file2Upload.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(i);
}
我尝试从浏览器下载文件,它开始下载,但总是失败。这个服务有什么问题导致下载失败吗?
HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=myDoc.docx");
- fateddyResourceHttpMessageConverter
实例添加到您的HttpMessageConverters
列表中。创建一个继承WebMvcConfigurerAdapter
的@Configuration
类,实现configureMessageConverters()
方法,并添加converters.add(new ResourceHttpMessageConverter());
这行代码。 - ashario