以流的形式访问远程图片

时间:2022-08-25 20:35:18 阅读: 最新文章 文档下载
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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;

本文来源:https://www.wddqw.com/doc/0eb4afcd08a1284ac8504368.html