Results 1 to 6 of 6
Thread: CONVERTING BYTE ARRAY to STRING
- 01-15-2012, 01:19 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
CONVERTING BYTE ARRAY to STRING
Hi,
im trying to convert byte array to string as below.
String newString= new (bytearray);
But whats happening is all the data in bytearray is not being converted to string.
The byte array contains an image of approx 2 mb. Should nt the bytearray length be same as the string length.
When running the code in my local machine i get identical values of bytearray.length and newString.length().
But while deploying the code in unix server i find that newString.length is considerably less than bytearray.length.
What could be going wrong here?
- 01-15-2012, 02:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: CONVERTING BYTE ARRAY to STRING
Usually an image doesn't contain characters; if you try to convert the byte sequence of an image as if they were encoded unicode characters, everything goes berzerk; and that is ignoring the encoding of the characters in the image (which aren't there). Don't try to convert the byte sequence to a character sequence.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-15-2012, 05:51 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: CONVERTING BYTE ARRAY to STRING
Thanks Jon.....
Let me tell you what im trying to do.
Im uploading a file and saving it in DB as blob. After this i read the blob as bytes.Before writing the byte array i convert the byte array into string to find the starting and end position of the file by searching for the content type and then use write method to send the file back to user.
outputstream.write(bytearray,startposition,lengtho ffile);
I tried this running the servlet in my local machine.This way it worked well but when i deployed the code in remote unix machine i was nt able to get the correct start and end position of the file since the conversion of byte array into string did not work properly.
Im not sure if my requirement can be performed without converting the bytes to string.
Any help is appreciated...
- 01-15-2012, 06:44 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: CONVERTING BYTE ARRAY to STRING
As I wrote before: don't convert any bytes to characters because they aren't characters. Simply read the bytes from your file and write them to a byte array (as ByteArrayOutputStream is ideal for that). Next write your byte array to the blob. A blob is very well capable to remember the number of bytes in it. Later you can read the blob back in again in a byte array, construct a ByteArrayInputStream for it and feed that stream to anything that can construct an image out of it again.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-18-2012, 05:57 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: CONVERTING BYTE ARRAY to STRING
Thanks a lot dude...
I got this straight at last. I tried not coverting the file to String. But that had other problems.
I ll write down my exact problem and solution so that it might help others...
My requirement was that i had to upload a file save it in DB using the file name as primary key, and read the file and display it to the user when he desires.
I used a simple input tag in to select the file , used ENCTYPE="multipart/form-data" in the form tag.
Now when i click on the save button i call the submit method which sends the POST request which will contain the file data.
At the servlet end i now need to extract the file from the request.
DataInputStream input = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
Here now we have the file+ related data in the dataBytes array.
I extract the content type from the request and save that along with the byteArray in the DB.
While reading the file i have the byteArray +contentType.
Using the contentTyepe i have saved, i can find the start and end position of the exact file content.
Inorder to find the exact start and end position i need to convert the byteArray into String
String file=new String (byteArray);
once i have the start and end position i can write the file as below
FileOutputStream fileOut = new FileOutputStream(filename);
fileOut.write(dataBytes, StartPos, EndPos-StartPos);
My problem was that i was not getting the entire byteArray converted to string probably because it was not decoded correctly.From what i could find the JVM uses a default charset to decode the byte to string. I had the conversion problem only in unix system.
I used the below code to force java to use the same charset irrespective of the default jvm preference.
Charset charset = Charset.forName("ISO-8859-1");
String data =new String (byteArray,charset);
- 01-18-2012, 06:08 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: CONVERTING BYTE ARRAY to STRING
Do not save the content type with the byte array.
Stick it in its own field in the database.
Also I am really not convinced by that reading loop.
It normally takes the form:
Pass this stream into your database layer where you will bind it to your PreparedStatement using setBinaryStream().Java Code:InputStream is = request.getInputStream();
No error-prone faffing involved.
Similar Threads
-
A simple question about converting byte array to unicode string
By Genom in forum New To JavaReplies: 6Last Post: 02-17-2011, 01:22 PM -
Converting string to byte[]
By bobo67 in forum New To JavaReplies: 12Last Post: 09-10-2010, 09:10 PM -
converting byte array to bmp file
By Moorkh in forum New To JavaReplies: 2Last Post: 09-07-2010, 02:58 PM -
Need help converting int to a 4 byte array
By kook04 in forum Advanced JavaReplies: 5Last Post: 02-26-2010, 08:59 PM -
Converting Image to byte array[] ?
By afflictedd2 in forum CLDC and MIDPReplies: 0Last Post: 04-11-2009, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks