本文介绍了当outputBuffer bytesWritten<时,响应无法返回图像. 8kb的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SpringBoot应用程序中,我尝试通过以下方式通过Response对象的outputBuffer返回图像:
Within a SpringBoot app, I am attempting to return images via a Response object's outputBuffer, via:
try {
response.setContentType("image/png");
InputStream in = new FileInputStream(pathToFile);
IOUtils.copy(in, response.getOutputStream());
}
catch (Exception e){
...
}
这很好,除非图像小于8kb ,在这种情况下,它什么也不会返回.
This works fine, unless the image is less than 8kb, in which case it just returns nothing.
谁能告诉我为什么小于8kb的原因导致响应实际上返回零数据(并且-至关重要的-如何解决此问题)?
Can anyone tell me why being less than 8kb would cause the Response to actually return zero data (and - crucially - how to remedy this)?
推荐答案
我已经通过在标头中显式设置内容长度来解决它:
I've solved it by setting the content length explicitly in the 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,我不希望不知道内容的大小.
I guess it didn't like not knowing the size of the content if it was below 8kb.
这篇关于当outputBuffer bytesWritten<时,响应无法返回图像. 8kb的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!