This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I am here with question on usage of base64 class from apache common-codecs.
My code is like this:
What I supply here is byte array of a base 64 binary encoded image, I expected to recieve another byte array with array lenght slightly reduced.
But instead I recieve a byte array of size say, input length is 1020 bytes then out put will be an aryay of size 2 !
Is there any size limit that we should consider while passing argument to decodebase64() method?
I have read in the api doc that the default line size is 76 and '\n\r' are the default line breaks. Do I need to consider any configuration related to this?
Hope I have provided enough details regarding the problem...
Please guide me with your valuable answers/suggestions in this regard.
What I supply here is byte array of a base 64 binary encoded image, I expected to recieve another byte array with array lenght slightly reduced.
When you say you received another byte array, what is the first byte array? Is that the original byte array of the image, which you then encoded using Base64 and trying to decode using Base64.decodeBase64()?
I think you meant "base64 encoded string created out from byte[]".
Cheers,
Naren (SCJP, SCDJWS and SCWCD)
aravind kanganar
Ranch Hand
Joined: Nov 08, 2009
Posts: 36
posted
0
Hi Lester,
I am trying to develop a web service that takes image as attachment.
In the xsd, in order to represent 'inline attachment ' I defined a type using 'base64Binary'.
Now the java class corresponsing to the above mentioned type return byte[].
I tried to decode this byte array using decodeBase64() method.
And the decrypted out put is not same as original file.
You post is ambiguous atleast for me. I can't figure out how your actual web service is written (e.g you mentioned decryt after decode, are you encryting at the service end? what web service engine you are using? This is important for the client to use Decoding class).
I can guess you might not be using same library encoding and decoding classes at the service and client ends respectively.
If your objective is to retrieve image from service end to client end, I don't know why you use decoding! Your web service engine on the client side would take care of that. The underlying base64 encoding and decoding are done by the web service engine. Just retrieve the byte[] on client end and convert into expected file format. That should be it!
aravind kanganar
Ranch Hand
Joined: Nov 08, 2009
Posts: 36
posted
0
Hi there,
First of all apologies for the ambiguous post
Hope this will be clear:
1. I have a web service, to which one can send image file as an attachment with the request.
2. The attachment is supposed to be encoded into base64 format, which is happening (I am using Soap UI for sending the request).
3. For the web service I am using spring web service engine here, and a Marshalling payload endpoint.
4. The unmarshalled request object, returns a byte array for the attached image.
5. I am trying to decode this byte array hoping I will recover the original binary content, but this is not happening.
6. For decoding I am using static method: decodeBase64(byte[] base64encoded) from class Base64 (commons-codec API).
For the web service I am using spring web service engine here, and a Marshalling payload endpoint.
That explains!
Try encoding using Spring API class org.springframework.webflow.util.Base64 and remember you don't need to decode at the service end as web service engine does decoding and gives you byte[].
aravind kanganar
Ranch Hand
Joined: Nov 08, 2009
Posts: 36
posted
0
You are right Naren!!
I was actually storing the 'supposed to be encrypted image' (based on my assumption that the unmarshalled object is giving encrypted byte array) , recived at the endpoint, in a jpg file. Which when I opened appeared scrambled.
The reason for it is that I used to convert the byte array to string (by new String(byteArray) ) and then write it into file using BufferedWriter.
Instead of buffered writer I changed it to FileOutputStream and wrote the byte stream directly, which gave me exact image. And that worked