我已经实现了一个Spring Boot应用程序,它将来自GET请求的图像返回到getimage API,它适用于具有不同内容的图像,但不适用于图像为单色的情况。我可以在文件系统上看到图像正常,它不会返回。
用于返回图像的代码的通用版本如下:
public ResponseEntity<String> getImage(String pathToFile, HttpServletRequest request, HttpServletResponse response){
if (new File(pathToFile).exists()){
try {
response.setContentType("image/png");
InputStream in = new FileInputStream(pathToFile);
IOUtils.copy(in, response.getOutputStream());
}
catch (Exception e){
response.setContentType("text/plain");
return new ResponseEntity<String>("ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
return new ResponseEntity<String>("", HttpStatus.OK);
}
来自SUCCESSFUL请求的响应头等(取自JMeter):
Thread Name: Thread Group 1-1
Sample Start: 2016-03-02 15:51:26 GMT
Load time: 2044
Connect Time: 1
Latency: 4
Size in bytes: 310323
Headers size in bytes: 150
Body size in bytes: 310173
Sample Count: 1
Error Count: 0
Response code: 200
Response message: OK
Response headers:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: image/png;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 02 Mar 2016 15:51:26 GMT
HTTPSampleResult fields:
ContentType: image/png;charset=UTF-8
DataEncoding: UTF-8
来自FAILED请求的响应标头,带有单色图像:
Thread Name: Thread Group 1-1
Sample Start: 2016-03-02 16:24:44 GMT
Load time: 5
Connect Time: 1
Latency: 5
Size in bytes: 2056
Headers size in bytes: 2056
Body size in bytes: 0
Sample Count: 1
Error Count: 0
Response code: 200
Response message: OK
Response headers:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Charset: big5, big5-hkscs, euc-jp, euc-kr, gb18030, gb2312, gbk, ibm-thai, ibm00858, ibm01140, ibm01141, ibm01142, ibm01143, ibm01144, ibm01145, ibm01146, ibm01147, ibm01148, ibm01149, ibm037, ibm1026, ibm1047, ibm273, ibm277, ibm278, ibm280, ibm284, ibm285, ibm290, ibm297, ibm420, ibm424, ibm437, ibm500, ibm775, ibm850, ibm852, ibm855, ibm857, ibm860, ibm861, ibm862, ibm863, ibm864, ibm865, ibm866, ibm868, ibm869, ibm870, ibm871, ibm918, iso-2022-cn, iso-2022-jp, iso-2022-jp-2, iso-2022-kr, iso-8859-1, iso-8859-13, iso-8859-15, iso-8859-2, iso-8859-3, iso-8859-4, iso-8859-5, iso-8859-6, iso-8859-7, iso-8859-8, iso-8859-9, jis_x0201, jis_x0212-1990, koi8-r, koi8-u, shift_jis, tis-620, us-ascii, utf-16, utf-16be, utf-16le, utf-32, utf-32be, utf-32le, utf-8, windows-1250, windows-1251, windows-1252, windows-1253, windows-1254, windows-1255, windows-1256, windows-1257, windows-1258, windows-31j, x-big5-hkscs-2001, x-big5-solaris, x-compound_text, x-euc-jp-linux, x-euc-tw, x-eucjp-open, x-ibm1006, x-ibm1025, x-ibm1046, x-ibm1097, x-ibm1098, x-ibm1112, x-ibm1122, x-ibm1123, x-ibm1124, x-ibm1364, x-ibm1381, x-ibm1383, x-ibm300, x-ibm33722, x-ibm737, x-ibm833, x-ibm834, x-ibm856, x-ibm874, x-ibm875, x-ibm921, x-ibm922, x-ibm930, x-ibm933, x-ibm935, x-ibm937, x-ibm939, x-ibm942, x-ibm942c, x-ibm943, x-ibm943c, x-ibm948, x-ibm949, x-ibm949c, x-ibm950, x-ibm964, x-ibm970, x-iscii91, x-iso-2022-cn-cns, x-iso-2022-cn-gb, x-iso-8859-11, x-jis0208, x-jisautodetect, x-johab, x-macarabic, x-maccentraleurope, x-maccroatian, x-maccyrillic, x-macdingbat, x-macgreek, x-machebrew, x-maciceland, x-macroman, x-macromania, x-macsymbol, x-macthai, x-macturkish, x-macukraine, x-ms932_0213, x-ms950-hkscs, x-ms950-hkscs-xp, x-mswin-936, x-pck, x-sjis_0213, x-utf-16le-bom, x-utf-32be-bom, x-utf-32le-bom, x-windows-50220, x-windows-50221, x-windows-874, x-windows-949, x-windows-950, x-windows-iso2022jp
Content-Type: text/plain;charset=UTF-8
Content-Length: 0
Date: Wed, 02 Mar 2016 16:24:44 GMT
HTTPSampleResult fields:
ContentType: text/plain;charset=UTF-8
DataEncoding: UTF-8
失败请求的空体似乎表明InputStream / IOUtils执行无法正常工作。如果我忘记包含任何内容,请告诉我。
编辑:我在图片编辑器中打开了有问题的非返回图片并在其上画了一个波浪线 - 现在下载完美。问题肯定是IOUtils为单个彩色图像复制流,但为什么呢?
我已将其缩小到以下奇怪的情况:如果图像为8kb,则不会返回。如果调整大小以使其为9kb +,则返回成功。 IOUtils.copy中是否有最小缓冲区或其他内容?
答案 0 :(得分:0)
我通过在标题中明确设置内容长度来解决它:
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){
...
}
}
这是我在此问到的问题的早期版本:Response fails to return image when outputBuffer bytesWritten < 8kb
可以重复关闭。