【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《以流的形式访问远程图片》,欢迎阅读!
public Object setImgeToByteArray(HttpServletRequest request, HttpServletResponse response,
@PathVariable("imageName") String imageName){
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
//imgeUrl 拼接图片绝对路径
String imgeUrl =ParamsBean.MANAGER_URL+"/upload/"+
imageName.substring(0,4)+"/"+imageName.substring(4,6)+"/"+
imageName+".jpg";
java.net.URL url;
java.net.URLConnection conn;
java.io.InputStream is;
ServletOutputStream out = null;
try {
url = new java.net.URL(imgeUrl);
conn = url.openConnection();
is = conn.getInputStream();
BufferedImage bu= ImageIO.read(is);
out = response.getOutputStream();
//把这个jpg图像写到这个流中去
ImageIO.write(bu, "jpg",out);
} catch (IOException e) {
log.debug("setImgeToByteArray==>"+e);
}finally {
try {
out.flush();
} catch (IOException e) {
log.error("图片转换成输出流失败", e);
} finally {
try {
out.close();
} catch (IOException e) {
log.error("图片流关闭失败", e);
}
}
}
return null;