i am trying to upload images and retrieving with help of Base64 encoding & decoding
as follows
decoding
if (imgString != null ) {
BufferedImage image = null;
byte[] imageByte;
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(imgString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
String realPath = request.getSession().getServletContext().getRealPath("/resources/images/"+studentDetails.getFirstname()+".png");
File outputfile = new File(realPath);
ImageIO.write(image, "png", outputfile);
studentDetails.setStuImg(outputfile.toString());
}
While decoding I am getting sometimes the following Exception
May 27, 2018 3:04:27 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/campasAdmin] threw exception [Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: Java heap space] with root cause java.lang.OutOfMemoryError: Java heap space
encoding
if (imagePath != null && imagePath.length() > 0) {
byte[] bytes = Files.readAllBytes(Paths.get(imagePath));
encodedFile = Base64.getEncoder().encodeToString(bytes);
}
What is the solution to it? How would I avoid it?