gpt4 book ai didi

浏览器中的 Java 输出图像质量差

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:52 25 4
gpt4 key购买 nike

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

读取图像:

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

关于浏览器中的 Java 输出图像质量差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20642032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com