我正在尝试将输入流图像写入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;
}
这是原始图片:
这是上述程序后的输出:
我的代码出了什么问题?
答案 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(); // <-----------