aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Using Apache Commons Codec Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Using Apache Commons Codec" Watch "Using Apache Commons Codec" New topic
Author

Using Apache Commons Codec

Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
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?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39573
    
  27
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?


Ping & DNS - updated with new look and Ping home screen widget
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
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
Marshal

Joined: Mar 22, 2005
Posts: 39573
    
  27
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.
John Eric Hamacher
Ranch Hand

Joined: Apr 25, 2007
Posts: 230
(apos)lt(semicolon)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Apache Commons Codec
 
Similar Threads
Binary file read/write Question
Sending a file over socket
Calling flush()
Transfer multiple files from Server to Client using Sockets
Writing input stream to file