Java输出图像浏览器质量不好

时间:2013-12-17 18:34:44

标签: java

我正在尝试将输入流图像写入OutputStream以在浏览器中显示图像,这是代码:

try
{
    InputStream input = Filer.readImage("images/test.jpg");
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buffer)) != -1)
    {
        responseBody.write(buffer, 0, bytesRead);
    }
}
catch(IOException e)
{
    System.out.println(e);
}

readImage:

public static InputStream readImage(String file) throws IOException {
    InputStream input = new FileInputStream(file);

    return input;

}

这是原始图片:

  

http://i.stack.imgur.com/zQzVG.jpg

这是上述程序后的输出:

  

http://i.stack.imgur.com/WgaKF.jpg

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:4)

您需要关闭输出流:

InputStream input = Filer.readImage("images/test.jpg");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
    responseBody.write(buffer, 0, bytesRead);
}
responseBody.close(); // <-----------