-
nginx方式
location /file/ {
alias /home/jclazz/80kj/smart-admin/uploadfile/image/;#指定图片存放路径
#access_log /usr/local/websrv/nginx-1.9.4/logs/images.log;#日志存放路径
} -
另一种方式
public ResponseEntity<byte[]> getImg(@PathVariable(name = "fileName") String fileName) {
log.error("预览图片 {}",fileName);
File file = new File(location + fileName);
if(!file.exists()){
return ResponseEntity.notFound().build();
}
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(fileName);
if(!mediaType.isPresent()){
return ResponseEntity.badRequest().build();
}
byte[] bytes = FileReader.create(file).readBytes();
return ResponseEntity.ok().contentType(mediaType.get()).body(bytes);
}