Java 使用 REST API 下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50026141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download File with REST API
提问by Nicolas HELIE
I try to develop an api to open a file like word or pdf. I test with swagger this API but I have the error below. The parameter given by swagger or the front are similar to this -> Z:\Some\Path\To\My\File.docx.
我尝试开发一个 api 来打开像 word 或 pdf 这样的文件。我用 swagger 这个 API 测试,但我有下面的错误。swagger 或前面给出的参数类似这样 -> Z:\Some\Path\To\My\File.docx。
I tried this pathFile on the explorateur windows and it's works fine.
我在探索者窗口上尝试了这个 pathFile 并且它工作正常。
STACK TRACE
堆栈跟踪
java.io.FileNotFoundException: InputStream resource [resource loaded through InputStream] cannot be resolved to absolute file path
at org.springframework.core.io.AbstractResource.getFile(AbstractResource.java:114) ~[spring-core-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at ... .DownloadDocumentResource.downloadFile(DownloadDocumentResource.java:28) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.11.RELEASE.jar:4.3.11.RELEASE]
REST CONTROLLER
休息控制器
@RestController
public class DownloadDocumentResource {
@ResponseBody
@RequestMapping(method = RequestMethod.POST, path = {"/download"})
public ResponseEntity<InputStreamResource> downloadFile(@RequestBody String pathFile) throws IOException {
out.println(pathFile);
InputStreamResource resource = new InputStreamResource(new FileInputStream( pathFile));
File fileToDownload = resource.getFile();
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileToDownload.getName())
.contentLength(fileToDownload.length())
.contentType(MediaType.parseMediaType(MediaType.APPLICATION_OCTET_STREAM_VALUE))
.body(resource);
}
采纳答案by oleg.cherednik
DownloadDocumentController
下载文件控制器
@RestController
@RequiredArgsConstructor
public class DownloadDocumentController {
private static final String APPLICATION_MS_WORD_VALUE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
private final DownloadDocumentService downloadDocumentService;
@PostMapping(value = "/download", produces = APPLICATION_MS_WORD_VALUE)
public ResponseEntity<byte[]> downloadFile(@RequestBody String pathFile) throws IOException {
byte[] content = downloadDocumentService.downloadFile(pathFile);
return ResponseEntity.ok()
.contentLength(content.length)
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_MS_WORD_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "File.docx"))
.body(content);
}
}
DownloadDocumentService
下载文件服务
@Service
public class DownloadDocumentService {
public byte[] downloadFile(String pathFile) {
// TODO do read available resource and create byte[]
return null;
}
}