简体   繁体   English

Rest API 返回 byte[] i 响应

[英]Rest API returning byte[] i response

What should be in the consumes and produces for Rest API which is returning byte[] of file in the response. Rest API 的消耗和生产中应该包含什么,它在响应中返回文件的字节 []。 No file params are included in the request .请求中不包含文件参数。

你可以使用下面的返回字节[]

@Produces(MediaType.APPLICATION_OCTET_STREAM)

You can use 'MultipartFile' for the purpose of consuming and sending back a file in response.您可以使用“MultipartFile”来消费和发回文件作为响应。

You can have a look at the following tutorial at spring.io for detailed tutorial: https://spring.io/guides/gs/uploading-files/您可以在 spring.io 上查看以下教程以获取详细教程: https : //spring.io/guides/gs/uploading-files/

Hope it helps!希望能帮助到你!

You should set the media type on basis of file content type.您应该根据文件内容类型设置媒体类型。

for example:例如:

  @GetMapping
  public HttpEntity returnByteArray() {
      String filepath = ; //filepath
      String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
      byte[] byteContent = ; //Content
      final HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.valueOf(contentType));
      return new HttpEntity(byteContent, headers);
}

OR或者

If you always return the same content file type then you can also set in如果您总是返回相同的内容文件类型,那么您也可以在

    @GetMapping(produces = "mime_type")
    public byte[] returnByteArray() {
      return new byte[0];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM