当 outputBuffer bytesWritten < 8kb 时,对 return 图像的响应失败
Response fails to return image when outputBuffer bytesWritten < 8kb
在 SpringBoot 应用程序中,我试图通过 Response 对象的 outputBuffer return 图像,通过:
try {
response.setContentType("image/png");
InputStream in = new FileInputStream(pathToFile);
IOUtils.copy(in, response.getOutputStream());
}
catch (Exception e){
...
}
这很好用,除非图像小于 8kb,在这种情况下它只是 return 什么都没有。
谁能告诉我为什么小于 8kb 会导致响应实际上 return 零数据(以及 - 关键 - 如何解决这个问题)?
我已经通过在 header 中明确设置内容长度解决了这个问题:
File actualFile = new File(pathToFile);
if (actualFile.exists()){
try {
response.setContentType("image/png");
response.setHeader("Content-Length", String.valueOf(actualFile.length()));
InputStream in = new FileInputStream(pathToFile);
IOUtils.copy(in, response.getOutputStream());
}
catch (Exception e){
...
}
}
我想如果内容小于 8kb,它不喜欢不知道内容的大小。
在 SpringBoot 应用程序中,我试图通过 Response 对象的 outputBuffer return 图像,通过:
try {
response.setContentType("image/png");
InputStream in = new FileInputStream(pathToFile);
IOUtils.copy(in, response.getOutputStream());
}
catch (Exception e){
...
}
这很好用,除非图像小于 8kb,在这种情况下它只是 return 什么都没有。
谁能告诉我为什么小于 8kb 会导致响应实际上 return 零数据(以及 - 关键 - 如何解决这个问题)?
我已经通过在 header 中明确设置内容长度解决了这个问题:
File actualFile = new File(pathToFile);
if (actualFile.exists()){
try {
response.setContentType("image/png");
response.setHeader("Content-Length", String.valueOf(actualFile.length()));
InputStream in = new FileInputStream(pathToFile);
IOUtils.copy(in, response.getOutputStream());
}
catch (Exception e){
...
}
}
我想如果内容小于 8kb,它不喜欢不知道内容的大小。