Results 1 to 6 of 6
  1. #1
    Sukeshps is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default 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?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Sukeshps is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default 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...

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Sukeshps is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default 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);

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default 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:
    Java Code:
    InputStream is = request.getInputStream();
    Pass this stream into your database layer where you will bind it to your PreparedStatement using setBinaryStream().
    No error-prone faffing involved.

Similar Threads

  1. Replies: 6
    Last Post: 02-17-2011, 02:22 PM
  2. Converting string to byte[]
    By bobo67 in forum New To Java
    Replies: 12
    Last Post: 09-10-2010, 10:10 PM
  3. converting byte array to bmp file
    By Moorkh in forum New To Java
    Replies: 2
    Last Post: 09-07-2010, 03:58 PM
  4. Need help converting int to a 4 byte array
    By kook04 in forum Advanced Java
    Replies: 5
    Last Post: 02-26-2010, 09:59 PM
  5. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-12-2009, 12:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •