//第一种方式:
/**
* 图片在本地磁盘的位置 如:E:/teacherCompetition/1/images/1.jpg
*
* @param imgName 图片名称
* @param response
*/
@GetMapping("/toImg")
public void picToPage(String imgName, HttpServletResponse response) {
FileInputStream in;
response.setContentType("application/octet-stream;charset=UTF-8");
try {
//图片读取路径
in = new FileInputStream(imgpath + imgName);
int i = in.available();
byte[] data = new byte[i];
in.read(data);
in.close();
//写图片
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//第二种方式
@GetMapping("/img/{fileName}")
public ResponseEntity<byte[]> download3(@PathVariable String fileName, HttpServletResponse response) {
//获取文件对象
try {
byte[] bytes = FileUtils.readFileToByteArray(new File(imgpath + fileName));
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Disposition", "attachment;filename=test2.png");
ResponseEntity<byte[]> entity = new ResponseEntity<>(bytes, headers, HttpStatus.OK);
return entity;
} catch (IOException e) {
}
return null;
}
get直接请求:http://localhost:7500/foundation/synopsisPro/img/4cc3fa9cbac0421b91e8dff86d1b2cb1.jpg