[JAVA] 특정 URL의 이미지스트림 가져오기 (Image URL to OutputStream)
이미지 URL을 아웃풋스트림으로 내려주기 위한 코드.
꼭 이미지가 아니더라도 스트림을 그대로 내려주기 위한 코드이다.
WAS가 DMZ에 있는 경우 해당 WAS를 통하지 않고는 이미지를 가져올 수 없을 때 사용.
WAS의 특정 URL을 호출했을 때 실제로는 변수 strUrl에 해당하는 이미지를 내려주는 코드이다.
|
public void getImgFromUrl(HttpServletResponse response, String strUrl) { if (strUrl == null || strUrl.length() == 0) { return; } InputStream inputStream = null; ServletOutputStream outputStream = null; try { inputStream = new URL(strUrl).openStream(); outputStream = response.getOutputStream(); int length; byte[] buffer = new byte[12288]; // 12K while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); }
outputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (outputStream != null) { outputStream.close(); } } catch (NullPointerException e) { } catch (Exception e) { } try { if (inputStream != null) { inputStream.close(); } } catch (NullPointerException e) { } catch (Exception e) { } } }
|
참고사이트 : https://stackoverflow.com/questions/8623709/output-an-image-file-from-a-servlet