使用getResponse()。getOutputStream()。write丢失图像的底部部分 [英] Missing Bottom part of image using getResponse().getOutputStream().write

查看:94
本文介绍了使用getResponse()。getOutputStream()。write丢失图像的底部部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从回复中得到了图片

PostMethod post = new PostMethod(action);
HttpClient httpClient = createHttpClient();

........

httpClient.executeMethod(post);


try {
            log.info("post successfully");
            String contentType = post.getResponseHeader("Content-type").getValue();
            int contentLength = (int) post.getResponseContentLength();
            byte[] responseBody = FileUtils.convertInputStreamtoByteArray(post.getResponseBodyAsStream());
            log.info("get response sucessfully : size "+ responseBody.length +" contentLength " + contentLength);
            return new ReturnBean(null, responseBody,contentType,contentLength);
        } catch (Exception e) {
            log.error(e.getMessage());
            log.error(e.getStackTrace());
            e.printStackTrace();
            throw new ResponseFailedException(e.getMessage());
        }

这就是我将输入流转换为字节数组的方式。

public static byte[] convertInputStreamtoByteArray(InputStream is){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            byte[] buf = new byte[1024];
            int i = 0;
            while ((i = is.read(buf)) >= 0) {
                baos.write(buf, 0, i);
            }
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return baos.toByteArray();
    }

这就是我返回图像作为响应的方式。

this is how i return the image as a response.

byte[] imageSource = (byte[])returnStream.getBean();
            log.info("imageSource " + imageSource.length);
            getResponse().setContentType((String) returnStream.getBean2());
            getResponse().setContentLength((Integer) returnStream.getBean3());
            getResponse().getOutputStream().write(imageSource);
            getResponse().getOutputStream().flush();

我能够打印出图像,但由于底部缺少而我遇到了问题。我检查了我得到的字节大小,它等于实际图像的大小。

i was able to print out the image but im having a problem because the bottom part of it is missing . i checked the size of byte that i got and it is equal to the size of actual image.

推荐答案

当我使用 IOUtils.copyLarge(); 而不是我的方法 convertInputStreamtoByteArray

when i used IOUtils.copyLarge(); instead of my method convertInputStreamtoByteArray

ServletOutputStream outputStream = getResponse().getOutputStream();
            InputStream inputStream = (InputStream) returnStream.getBean();
            IOUtils.copyLarge(inputStream , outputStream);

它有效。我不知道会发生什么,因为我前一阵子用过,但没有用。

it works . i dont know what happen because i used it a while ago and it didnt work.

这篇关于使用getResponse()。getOutputStream()。write丢失图像的底部部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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