• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Decoding a base64 image dataUrl to a jpeg image in Servlets  RSS feed

Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am trying to decode a base64 image dataURL to a jpeg image in a servlet & then save it.
The image dataURL I am getting from a web page.

I am using apache.commons.codec.binary.base64 jar

below is my code which I am able to write till now. Please help me how do I do it?

Ranch Hand
Posts: 187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, what is it you are trying to do? Display an image? Or process a user upload?

If you are trying to display the image, you cannot serve it out as a String as you do here:

Also, the image has to already exist if you want to display it.

If you are trying to let a user upload an image, you cannot process it in the doGet method, since those parameter values are passed in the URL. You have to do it in the doPost method. That's just the beginning, but there are other confusing, unclear things in your post. For example, why is the image data encoded as base 64? Where is this data coming from? There are plenty of tutorial out there on how to build an html form with a file upload field and how to extract the data in the servlet's doPost method, but I'm not sure these apply to your specific circumstances.
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OutputStream out = response.getOutputStream();

try {
ImageIO.write(image, "jpg", out);
} catch(IOException ioe) {
System.err.println("Error writing JPEG file: " + ioe);

Likely the snippet of code you may be looking for. Not sure about base64 as I hardly ever have delt with it...only in practice. However, in that example it is getting the output stream from the response; presumably uploading it would be the next step ( from a Servlet anyway). Instead of outputing a string to the console you write it to a buffered image or engineer some other way to create the first argument of the static method write from class ImageIO. The output stream is from a Servlet so in this example it is getting uploaded to a client browser.
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!