【问题标题】:Spring Boot Rest Response MultipartFile with additional fields带有附加字段的 Spring Boot Rest Response MultipartFile
【发布时间】:2021-11-12 22:13:47
【问题描述】:

我们有一个带有以下端点的 RestController

@PostMapping(path = "/downloadFile", produces = MediaType.MULTIPART_FORM_DATA_VALUE, 
                  consumes = MediaType.APPLICATION_JSON_VALUE)
public FileDownloadResponse downloadFile(@RequestBody FileDownloadRequest request) {
     FileDownloadResponse downloadResponse = new FileDownloadResponse();

     File file = new File("c:/fileLocation/"+request.getFileName());
     try (InputStream stream = new FileInputStream(file)) {
        byte[] bytes = IOUtil.toByteArray(stream);
        downloadResponse.setFileName(file.getName());
        downloadResponse.setCheckSum(calculateCheckSum(bytes));
        downloadResponse.setFileContents(new FileSystemResource(bytes, file.getName()));
     } catch (Exception e) {
        e.printStackTrace();
     }
     return downloadResponse;
}

public class FileDownloadResponse {
     private String fileName;
     private Long checkSum;
     private Resource fileContents;
     
}

public static class FileSystemResource extends ByteArrayResource {

    private String fileName;

    public FileSystemResource(byte[] byteArray , String filename) {
        super(byteArray);
        this.fileName = filename;
    }

    public String getFilename() { return fileName; }
    public void setFilename(String fileName) { this.fileName= fileName; }

}

在客户端,我们有以下代码,

public class FileDownloadResponseClient {
     private String fileName;
     private Long checkSum;
     private MultipartFile fileContents;     
}

public FileDownloadResponseClient download(FileDownloadRequest request) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Collections.singletonList(Mediatype.ALL));

    HttpEntity<FileDownloadRequest> requestEntity = new HttpEntity<>(request, headers);

    return restTemplate.postForEntity(downloadUrl, requestEntity, FileDownloadResponseClient.class);                                         

}

当我们运行上面的 Rest Client 时,我们收到以下错误, org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : [没有正文]

是否可以下载 multipartfile 以及其他附加字段?如果是,我们在这里缺少什么,请告诉我们。

提前致谢!

【问题讨论】:

    标签: java spring-boot spring-restcontroller spring-rest spring-resttemplate


    【解决方案1】:

    org.springframework.web.multipart 有一个方法 boolean isEmpty() 来查找文件是否没有内容。最好将检查放在那里并重定向到有关此类文件多部分表单的消息。 在 [no body] 中,我发现关于对 http 服务器的测试请求的消息,但总的来说,通常意味着表单中没有任何内容,或者服务器完成请求不需要额外的信息。 现在我假设 spring 框架处理上传文件的所有 url 解码和边界标记剥离(在两端)。

    【讨论】:

    • 文件存在,读取文件内容时服务器端没有异常。但是,在客户端,如果我们删除 headers.setAccept(Collections.singletonList(Mediatype.ALL)); 行,我们会得到一个 HttpStatus 406 'Not Acceptable',请注意这是从服务器下载文件以及其他一些附加属性。当我们只下载文件而没有任何其他附加字段时,它可以正常工作,换句话说,当restcontroller端点返回ResponseEntity&lt;Resource&gt;时,我们就可以下载它了。
    猜你喜欢
    • 2015-01-28
    • 2023-02-14
    • 2017-03-08
    • 2017-11-27
    • 2016-07-13
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多