使用REST API下载文件

时间:2018-04-25 15:31:09

标签: java rest inputstream

我尝试开发一个api来打开像word或pdf这样的文件。 我使用swagger测试这个API,但我有以下错误。 由swagger或front给出的参数与此类似 - > Z:\一些\路径\至\我\ File.docx

我在探索者窗口上尝试了这个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控制器

@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);
    }

1 个答案:

答案 0 :(得分:3)

<强> DownloadDocumentController

var data = JSON.stringify( payload );

const Prueba = () => {
  fetch(loginurl, {
    method: 'POST',
    headers: {
      "Content-Type": "application/json"
    },
    body: data
  })
  .then(response => response.json())
  .then(jsondata => console.log(jsondata))
  .catch(function(error) {
    console.log(error);
  })
}  

<强> DownloadDocumentService

@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);
    }
}