java - 使用 REST API 下载文件

标签 java rest inputstream

我尝试开发一个API来打开word或pdf等文件。 我用 swagger 测试了这个 API,但出现了以下错误。 swagger或者前面给的参数和这个类似 -> Z:\Some\Path\To\My\File.docx.

我在explorateur windows上尝试了这个pathFile,它工作正常。

堆栈跟踪

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]

休息 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);
    }

最佳答案

下载文档 Controller

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

下载文档服务

@Service
public class DownloadDocumentService {

    public byte[] downloadFile(String pathFile) {
        // TODO do read available resource and create byte[]
        return null;
    }

}

关于java - 使用 REST API 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50026141/

相关文章:

java - 将输入流重定向到套接字的输出流

java - Android 应用程序在启动时崩溃

Java:可以发送和接收UDP数据包,但接收到的数据是乱码

java - 改造 response.body() 为 null,但 API JSON 在 Logcat 中可见

php - Laravel 嵌套所有 json 响应

java - 在 InputStream 中搜索

java - InputStream 对象在声明后关闭

java - FileStore类的getUsableSpace和getUnallocatedSpace有什么区别

java - AbstractCommonPowerMockRunner 无法解析

rest - Angular 2 auth http header 为空