【发布时间】:2015-03-03 14:46:54
【问题描述】:
这快把我逼疯了!我正在尝试提供 JPG 图片。我确信这种方法前几天工作得很好,所以我不知道发生了什么变化。我已经尝试了很多不同的方法来让它工作,但我似乎无法摆脱异常。
基本上我正在尝试从数据库中提供图像。
我认为实际字节可能已损坏,因此我将它们写入文件,并检查了文件内容。就在 Mac 上的 Finder 中,临时目录中的文件在预览应用程序中看起来很好,所以我很确定不是内容本身导致了问题。
这是控制器方法:
@RequestMapping(value="/binaries/**", method = RequestMethod.GET, produces={MediaType.APPLICATION_JSON_VALUE, MediaType.IMAGE_GIF_VALUE,
MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE, "application/javascript"})
public @ResponseBody
ResponseEntity<byte[]> serveResource(WebRequest webRequest, HttpServletResponse response, String uri) throws IOException {
String path = (String)request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE );
BinaryFile bf = binaryService.findByUri(path);
String tmpdir = System.getProperty("java.io.tmpdir");
File dest = new File(tmpdir + File.separator + bf.getFileName());
FileUtils.writeByteArrayToFile(dest, bf.getResource());
logger.debug("file written: " + dest.getAbsolutePath());
// response.addHeader("Cache-Control", "public, max-age=3600");
if (webRequest.checkNotModified(bf.getLastModifiedDate().toDate().getTime()))
{
return null;
};
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(bf.getResource());
}
这是个例外:
Request: http://localhost:8080/binaries/products/shortcode_1/test_image2.jpg raised org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
有人有什么想法吗?这是春天 4.1.4.RELEASE
【问题讨论】:
标签: spring-mvc