• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using Apache Commons Codec

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My JSP page POST's an image to a servlet and I would like this servlet to write the image to the file system. I get the input stream from the request, read the stream and write the bytes to a file using FileOutputStream.

Now I need to convert this data back to binary format, otherwise the image cannot be displayed when I open the file. I'm using Apache Commons Codec, which I am not familiar with, and there is a method that seems to help


It accepts a byte array containing Base64 encoding and returns a byte array with binary data.

My existing code is


How would I use this API in my current code? As I'm using a fixed buffer size, I don't really want to read everything into the buffer, decode the array and then write the file from the buffer. I thought about having a second byte array to decode bytes each time the buffer is filled, but I'm getting an ArrayIndexOutOfBoundsException.



Is there a better way to do this?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now I need to convert this data back to binary format


I don't understand this. When does the image data cease to be binary - do you base64-encode it somewhere? Or is the uploaded image data encoded already?
 
Aaron John
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was my understanding that any binary data posted to a servlet would be base64-encoded, but let me explain the background. I scan a page in a scanner using a 3rd party API. It stores the image in its buffer and I invoke its POST methods to hit my servlet, which must be doing something behind the scenes.

When I uploaded the data, read the InputStream in the request and wrote the bytes to a file using FileOutputStream, the file couldn't open properly using a picture viewer. When I opened the file using a text editor, the first lines of the file contained

I assumed there was some encoding, and I would need to decode it in order to open it properly?

Or am I missing something?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of trying to roll your own file upload -and running into a number of problems that have been run into before-, I'd recommend to use standard file upload libraries like Jakarta Commons HttpClient on the client side, and Jakarta Commons FileUpload on the server side. Links and an introductory article can be found here.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(apos)lt(semicolon)
 
reply
    Bookmark Topic Watch Topic
  • New Topic