前端调用此接口获取图片:
public void getPersonPic(String picName, HttpServletResponse response) throws IOException {
ServletOutputStream outputStream = null;
try {
byte[] bytes = getPic(picName);
outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
if (outputStream != null) {
outputStream.close();
}
}
}
public byte[] getPic(String picName) {
//进行图片路径处理。拼接上图片名
final Path path = Path.of(System.getProperty("user.dir"),"/home","/pic",picName);
byte[] bytes = null;
if (path.toFile().exists()) {
try {
bytes = Files.readAllBytes(path);
} catch (Exception e) {
}
}else {}
return bytes;
}