繁体   English   中英

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

[英]Java output image bad quality in browser

我正在尝试将输入流图像写入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

我的代码出了什么问题?

您需要关闭输出流:

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(); // <-----------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM