* * 设置浏览器打开文件所采用的编码
* response.setHeader("Content-Type", "text/html;charset=UTF-8");
* * 简写方式
* response.setContentType("text/html;charset=UTF-8");
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据
byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据
return btImg;
@GetMapping("/image/{filename}")
public ResponseEntity
File file = new File(filename);
byte[] imageBytes = ImageUtils.getImageBytes(file);
ByteArrayResource resource = new ByteArrayResource(imageBytes);
return ResponseEntity.ok()
.contentType(MediaType.IMAGE_JPEG)
.contentLength(imageBytes.length)
.body(resource);
}