Wednesday, December 10, 2014

Write image to DB

Encode

private static String setBase64EncodedImage(File  Img) throws IOException {
FileInputStream fin = null;

try {
fin = new FileInputStream( Img);

return new String(Base64.encodeToByte(IOUtils.toByteArray(fin),false));

} catch (FileNotFoundException e) {
LOGGER.error("Error  " + e);
} finally {
try {
if (fin != null) {
fin.close();
}
} catch (IOException ioe) {
throw new RuntimeException("context", ioe);
}
}
return null;
}

Decode

public String displayPushItem() throws WebException {

String pushid = getPushItemId();
try {

if (pushid != null) {
CCRPushItem pushItems = getCcrPushBA().getCCRPushItemWithImage(new BigDecimal(pushid));

if (pushItems != null) {
byte[] imgBuf = Base64.decodeFast(pushItems.getImg());
this.response.setContentType("image/jpg");
this.response.setContentLength(imgBuf.length);
this.response.getOutputStream().write(imgBuf, 0, imgBuf.length);
this.response.getOutputStream().flush();
this.response.getOutputStream().close();
}

}

} catch (Exception e) {
throw new WebException(e);
}
return null;

}
interceptor


               
               
           

No comments:

Post a Comment