我正在尝试将输入流图像写入 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;
}
这是原始图像:
这是上述过程后的输出:
我的代码有什么问题?
最佳答案
您需要关闭输出流:
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/