图片地址中转

// 图片文件中转接口
@RequestMapping(value = "/images/{guid}", method = { RequestMethod.GET })
public void getIcon(@PathVariable("guid") String guid, HttpServletRequest request, HttpServletResponse response)
throws IOException {
URL url = new URL(ImgAddressUtils.URL_imgAddress + guid + "?isView=false");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
System.out.println("Connection Response From Test Servlet");
InputStream inputStream = urlConnection.getInputStream();
// 响应
response.setContentType("image/png");
OutputStream stream = response.getOutputStream();
stream.write(input2byte(inputStream));
stream.flush();
stream.close();
}
public static final byte[] input2byte(InputStream inStream) throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[10240];
int rc = 0;
while ((rc = inStream.read(buff, 0, 10240)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
}
posted @ 2016-10-28 10:18  Logan_626  阅读(511)  评论(0编辑  收藏  举报