在春季 restcontroller 响应中返回 ByteArrayResource



我正在尝试使用我的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);
}

似乎有什么问题? 我该如何解决这个问题?

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

尝试返回字节数组:

@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

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

以下代码片段返回由 Web 浏览器显示的图像内容。这是一个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();
}

相关内容

  • 没有找到相关文章

最新更新