서버의 image data를 client에서 보여주기.
DB or 서버 파일에 Image 데이터를 GWT의 image 객체에 보여줘야하는 경우가 있다.
image를 inputstream으로 읽어봐서 client에 rpc를 이용해서 보내지는 못한다.
이럴 때 Base64를 사용하면 된다.
서버측 코드
private static String convertToBase64(InputStream in) throws IOException {
String base64 = Base64.encodeBase64String(IOUtils.toByteArray(in));
String imgBase64="https://t1.daumcdn.net/cfile/tistory/214FEB3856EA362E2B"+base64;
return imgBase64;
}
InputStream은 Image를 읽은 데이터이다.
이것을 Byte Array로 변겨해준다.
IOutils : org.apache.commons.io.IOUtils
Base64 : org.apache.commons.net.util.Base64
client 코드.
public class ServerImage extends Image {
public ServerImage(String imageUrl, String width, String height) {
String styleName = this.getStyleName();
this.setStyleName(styleName);
this.setUrl(imageUrl)
this.setSize(width, height);
}
}
빨갠색 부분이 중요하다.