CopyPastor

Detecting plagiarism made easy.

Score: 1.6980599761009216; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2018-12-07
by Shubham Mittal

Original Post

Original - Posted on 2014-07-24
by melkysalem



            
Present in both answers; Present only in the new answer; Present only in the old answer;

you can use this method to convert a image to base64 string
public static String encodeToString(BufferedImage image, String type) { String imageString = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, type, bos); byte[] imageBytes = bos.toByteArray(); BASE64Encoder encoder = new BASE64Encoder(); imageString = encoder.encode(imageBytes); bos.close(); } catch (IOException e) { e.printStackTrace(); } return imageString; Decode Base64 String to Image
public static BufferedImage decodeToImage(String imageString) {
BufferedImage image = null; byte[] imageByte; try { BASE64Decoder decoder = new BASE64Decoder(); imageByte = decoder.decodeBuffer(imageString); ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); image = ImageIO.read(bis); bis.close(); } catch (Exception e) { e.printStackTrace(); } return image; }
You can store your images in your database, but it's not advisable and bad practice.
Hello Programmer at first, you'll need to think what's the size(width and height) that you'll use, it's for a little profile photo? it's for a big album photo? I can advise you that if you want to store small images in database, that's ok, to me, but big images must be saved in disk accessed by an url.
When I need to do it, I use a base64 image converted and stored in the datababase like a String, vou VarChar, it depends of your database, but with it you'll need to convert to base64 on upload, and re-convert to image back on show, it's not a problem to current computers.
With this function, we can convert a BufferedImage(most possible to be got by a upload service) to a String, after this we can store it on a DB.
public static String encodeToString(BufferedImage image, String type) { String imageString = null; ByteArrayOutputStream bos = new ByteArrayOutputStream();
try { ImageIO.write(image, type, bos); byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder(); imageString = encoder.encode(imageBytes);
bos.close(); } catch (IOException e) { e.printStackTrace(); } return imageString; }
and with this method we can convert a String back to a BufferedImage, and show, or serve, on a jps file.
public static BufferedImage decodeToImage(String imageString) {
BufferedImage image = null; byte[] imageByte; try { BASE64Decoder decoder = new BASE64Decoder(); imageByte = decoder.decodeBuffer(imageString); ByteArrayInputStream bis = new ByteArrayInputStream(imageByte); image = ImageIO.read(bis); bis.close(); } catch (Exception e) { e.printStackTrace(); } return image; }
obviusly my answer don't envolves all aspects to store on databases, or how jps works with java, but it's only my advice.
Thanks to read it.

        
Present in both answers; Present only in the new answer; Present only in the old answer;