在试验输出图片显示到浏览器的时候出现了问题。
正确的代码:
@GetMapping("/getImg/{imgname:.+}")
public ResponseEntity getImg(@PathVariable String imgname) {
Path path = Paths.get("d:\\", imgname);
Resource resource = resourceLoader.getResource("file:" + path.toString());
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(resource);
}
错误的代码:
@RequestMapping(method = RequestMethod.GET, value = "/showImg/{filename:.+}")
public ResponseEntity getFile(@PathVariable String filename) {
Path path = Paths.get("d:\\", filename);
Resource resource = resourceLoader.getResource("file:" + path.toString());
// 都是这句json惹的祸,页面输出不了
logger.info("resource:{}",JSON.toJSONString(resource));
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(resource);
}
这段代码,输出地址后,页面不展示图片。
而且本地的图片也无法查看,而且无法删除,提示java程序正在使用该图片。
比对了变天,发现就多了一句Json.toJSONString()。
这是为什么呢?