在 Spring RestController 响应中返回 ByteArrayResource

问题描述 投票:0回答:2

我正在尝试使用我的restController 下载文件,但它总是返回此错误:

 java.io.FileNotFoundException: Byte array resource [resource loaded from byte array] cannot be resolved to URL
at org.springframework.core.io.AbstractResource.getURL(AbstractResource.java:90) ~[spring-core-4.2.2.RELEASE.jar:4.2.2.RELEASE]

然后它会下载一个包含以下内容的文件:

  {"byteArray":"JVBERi0xLjQKJeL.... 

这是我的休息控制器:

@Api("products")
@RestController
@RequestMapping("/v1/products")
public class DocumentApi extends storeApi {
    @ApiOperation("GET download document")
    @RequestMapping(value = "/temp", method = RequestMethod.GET)
    @ResponseStatus(code = HttpStatus.OK)
    public ResponseEntity<ByteArrayResource> downloadDocument(
        @RequestParam(value = "id", required = true) Long idInscription) throws IOException {
          String signedFilePAth = "C:/APPLIS/signedTemp/5982312957957647037_signed.pdf"
          return ResponseEntity
                 .ok()
                 .contentLength(contentLength)
                 .contentType(
                     MediaType.parseMediaType("application/pdf"))
                 .body(new 
          ByteArrayResource(Files.readAllBytes(Paths.get(signedFilePAth))));
    }
}

这是我的弹簧配置:

protected MappingJackson2HttpMessageConverter jacksonMessageConverter() {
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    // Registering Hibernate4Module to support lazy objects
    mapper.registerModule(new Hibernate4Module());
    messageConverter.setObjectMapper(mapper);
    return messageConverter;
}

public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
    ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
    arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
    return arrayHttpMessageConverter;
}

private List<MediaType> getSupportedMediaTypes() {
    List<MediaType> list = new ArrayList<MediaType>();
    list.add(MediaType.APPLICATION_OCTET_STREAM);
    list.add(MediaType.parseMediaType("application/pdf"));
    return list;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(jacksonMessageConverter());
    converters.add(byteArrayHttpMessageConverter());
    super.configureMessageConverters(converters);
}

问题出在哪里?我该如何解决这个问题?

nb:我不知道这是否相关,但我的 swagger-ui.html 不起作用(它显示空白页面),而 v2/api-docs/ 工作正常

java spring-mvc swagger spring-restcontroller spring-rest
2个回答
0
投票

尝试返回字节数组:

@RequestMapping(value = "/temp", method = RequestMethod.GET)
public @ResponseBody byte[] downloadDocument(
        @RequestParam(value = "id", required = true) Long idInscription) throws IOException {
    FileInputStream signedFileInputStream = new FileInputStream(signedFilePAth);
    byte[] doc = IOUtils.toByteArray(fis);
    return doc;
}

IOUtils 来自

org.apache.commons.io.IOUtils


0
投票

您的代码中似乎缺少响应内容类型定义。

以下代码片段返回网络浏览器显示的图像内容。这是一个 Jersy 代码,但您可以将其采用到 Spring 中:

@GET
@Path("/{image-uuid}")
@Produces("images/jpg")
public Response getImage(@PathParam("uuid") final String uuid) throws IOException {
    byte[] content = imageDao.getImage(uuid);
    if (Objects.isNull(content )) {
        throw new ImageNotFoundError(uuid);
    }

    ByteArrayInputStream stream = new ByteArrayInputStream(content);
    return Response.ok(stream).build();
}
© www.soinside.com 2019 - 2024. All rights reserved.