春季返回图像作为ResponseEntity< byte []>. -图像损坏 [英] Spring returning image as ResponseEntity<byte[]> - image corrupt

查看:79
本文介绍了春季返回图像作为ResponseEntity< byte []>. -图像损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Spring 3.2.7应用程序,它通过一个spring控制器将存储在数据库中的签名作为base64字符串发送回用户浏览器,该控制器输出字节数组ResponseEntity.

I am working on a spring 3.2.7 app and it sends signatures stored in the database as base64 string back to the users browser via a spring controller which outputs the byte array ResponseEntity.

图像始终被破坏,并且在我再次检查svn时我还没有在系统的这一部分上工作,并且自从创建我正在处理的分支以来,尚未触摸控制器.

The image is always corrupted and I havent worked on this part of the system as I double checked in svn and the controller has not been touched since the branch I am working on was created.

我能够将base64字符串转换为台式机上的图像,并且还可以在弹簧插入之前将返回给浏览器的字节数组转换为图像.

I am able to convert the base64 string to an image on my desktop and I am also able to the convert the byte array returned to browser into an image before spring steps in.

下面是我的代码,这显然是在以前工作的,所以也许有些配置更改可能会导致这种情况?

Below is my code, this was apparently working before so perhaps there is some config change that could cause this?

  @RequestMapping(value = "/submissions/signature/{type}/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> getSignature(@PathVariable String type, @PathVariable Integer id) throws Exception {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   String base64 = ... gets from db

   byte[] bytes = Base64.decodeBase64(base64);

    BufferedImage bi = ImageIO.read(new ByteArrayInputStream(bytes));
    ImageIO.write(bi, "png", baos);

    HttpHeaders headers = new HttpHeaders();
    headers.setLastModified(Calendar.getInstance().getTime().getTime());
    headers.setCacheControl("no-cache");
    headers.setContentType(MediaType.IMAGE_PNG);
    headers.setContentLength(baos.toByteArray().length);

    //Image as base64 string is ok in converter
    System.out.println("BASE 64 IMAGE IS: " + base64);
    //This image is created ok on desktop
    FileOutputStream fos = new FileOutputStream("C:\\Users\\p\\Desktop\\test_signature.png");
    fos.write(bytes);
    fos.close();
    //This image is created ok on desktop
    FileOutputStream fos3 = new FileOutputStream("C:\\Users\\p\\Desktop\\test_signature_baos.png");
    fos3.write(bytes);
    fos3.close();

    return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.OK);
   }

图像在浏览器中的呈现方式如下:

The image is being rendered in the browser like:

   <img id="userSignature" width="296" height="110" style="border:0px" src="/webapp/service/submissions/signature/user/${subid}" alt="User signature" />

我没有更改此类,但我被告知它确实起作用,我能够从两个字节数组创建图像,并且它们都可以并且看起来相同,并且我可以将签名字符串呈现为可以进行测试,例如:

I have not changed this class and I am told that it did work, I am able to create images from both byte arrays and they are ok and looks the same and I able to render the signature string ok for testing like:

  <IMG SRC="data:image/png;base64, <base_64_string>" ALT=""> 

有人遇到过类似的问题,还是知道是什么原因造成的?

has anyone experienced similar issues or know what could be causing this?

我现在已经尝试从已经创建为png的文件系统中发送图像,并且该操作也失败了.

I have now tried sending an image from my file system already created as png and that also fails.

我现在注意到CSV文件无法在应用中正确下载,并且它们以相同的方式流式传输:

I have now noticed that CSV files do not download properly in the app and they stream in the same way:

       @RequestMapping(value = "/results/csv", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> getResultsInCsvFormat() throws IOException {

推荐答案

我已经在InputStream的帮助下成功返回了文件内容:

I have successfully returned file contents with the help of an InputStream:

@RequestMapping(value = "/submissions/signature/{type}/{id}", 
                method = RequestMethod.GET)
public HttpEntity getFile(HttpServletResponse response,
                          @PathVariable String type, 
                          @PathVariable Integer id) {
    String base64 = "foo"; // get base-64 encoded string from db
    byte[] bytes = Base64.decodeBase64(base64);
    try (InputStream inputStream = new ByteArrayInputStream(bytes)) {
        StreamUtils.copy(inputStream, response.getOutputStream());
        response.setContentType(MediaType.IMAGE_PNG_VALUE);
    } catch (IOException e) {
        // handle
    }
    return new ResponseEntity(HttpStatus.OK);
}

请注意,我没有使用ResponseBody,在我的工作版本中,我使用的是MediaType.APPLICATION_OCTET_STREAM_VALUE,而不是实际的文件内容类型.

Note that I'm not using ResponseBody, and in my working version I am using MediaType.APPLICATION_OCTET_STREAM_VALUE rather than the actual file content type.

这篇关于春季返回图像作为ResponseEntity&lt; byte []&gt;. -图像损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆