如何在Spring Boot控制器中返回图像并像文件系统一样提供

时间:2016-11-11 23:13:00

标签: java spring-mvc spring-boot

我已经尝试过Stackoverflow中给出的各种方法,也许我错过了一些东西。

我有一个Android客户端(其代码我无法更改),目前正在获取如下图像:

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

其中url是图像的位置(CDN上的静态资源)。现在我的Spring Boot API端点需要以相同的方式表现得像文件资源,以便相同的代码可以从API获取图像(Spring引导版本1.3.3)。

所以我有这个:

@ResponseBody
@RequestMapping(value = "/Image/{id:.+}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> getImage(@PathVariable("id")String id) {
    byte[] image = imageService.getImage(id);  //this just gets the data from a database
    return ResponseEntity.ok(image);
}

现在,当Android代码尝试获取http://someurl/image1.jpg时,我的日志中出现此错误:

  

解决处理程序中的异常[public   org.springframework.http.ResponseEntity   com.myproject.MyController.getImage(java.lang.String中)〕:   org.springframework.web.HttpMediaTypeNotAcceptableException:不能   找到可接受的代表

http://someurl/image1.jpg插入浏览器时会发生同样的错误。

奇怪的是,我的测试结果还可以:

Response response = given()
            .pathParam("id", "image1.jpg")
            .when()
            .get("MyController/Image/{id}");

assertEquals(HttpStatus.OK.value(), response.getStatusCode());
byte[] array = response.asByteArray(); //byte array is identical to test image

如何使其行为像正常方式提供的图像一样? (注意我无法更改android代码发送的内容类型标题)

修改

评论后的代码(设置内容类型,取出produces):

@RequestMapping(value = "/Image/{id:.+}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public ResponseEntity<byte[]> getImage(@PathVariable("id")String id, HttpServletResponse response) {
    byte[] image = imageService.getImage(id);  //this just gets the data from a database
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    return ResponseEntity.ok(image);
}

在浏览器中,这似乎只是给出了一个字符串化的垃圾(字节到字符我猜)。在Android中它没有错误,但图像没有显示。

4 个答案:

答案 0 :(得分:16)

我相信这应该有效:

@RequestMapping(value = "/Image/{id:.+}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getImage(@PathVariable("id") String id) {
    byte[] image = imageService.getImage(id);
    return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
}

请注意,内容类型设置为ResponseEntity,而不是直接设置为HttpServletResponse

答案 1 :(得分:8)

最后解决了这个问题......我必须在我的ByteArrayHttpMessageConverter子类中添加WebMvcConfigurerAdapter

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    final ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
    final List<MediaType> list = new ArrayList<>();
    list.add(MediaType.IMAGE_JPEG);
    list.add(MediaType.APPLICATION_OCTET_STREAM);
    arrayHttpMessageConverter.setSupportedMediaTypes(list);
    converters.add(arrayHttpMessageConverter);

    super.configureMessageConverters(converters);
}

答案 2 :(得分:7)

如果你不知道文件/ mime类型,你可以这样做....我已经完成了这个,我拿了一个上传的文件并用guid替换文件名,没有扩展名和浏览器/智能手机能够加载图像没有问题。  第二个是提供要下载的文件。

@RestController
@RequestMapping("img")
public class ImageController {

@GetMapping("showme")
public ResponseEntity<byte[]> getImage() throws IOException{
    File img = new File("src/main/resources/static/test.jpg");
    return ResponseEntity.ok().contentType(MediaType.valueOf(FileTypeMap.getDefaultFileTypeMap().getContentType(img))).body(Files.readAllBytes(img.toPath()));
}
@GetMapping("thing")
public ResponseEntity<byte[]> what() throws IOException{
    File file = new File("src/main/resources/static/thing.pdf");
    return ResponseEntity.ok()
            .header("Content-Disposition", "attachment; filename=" +file.getName())
            .contentType(MediaType.valueOf(FileTypeMap.getDefaultFileTypeMap().getContentType(file)))
            .body(Files.readAllBytes(file.toPath()));
}


}   
在java 9+中

更新您需要将compile 'com.sun.activation:javax.activation:1.2.0'添加到您的依赖项中,这也已被jakarta移动或选取。see this post

答案 3 :(得分:0)

使用Apache Commons,您可以执行此操作并将图像公开在端点上

@RequestMapping(value = "/image/{imageid}",method= RequestMethod.GET,produces = MediaType.IMAGE_JPEG_VALUE)
public @ResponseBody byte[] getImageWithMediaType(@PathVariable int imageid) throws IOException {
    InputStream in = new ByteArrayInputStream(getImage(imageid));
    return IOUtils.toByteArray(in);
    }

所有图片将在端点/image/{imageid}投放