Results 1 to 19 of 19
  1. #1
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Question saving bytes received in different request

    Hi All,

    Im new to forum and to java . Im devloping an application that uses HttpServlet for receiving request from mobile device.

    Im uploading an image from mobile to server and saving it on server.

    I am accepting one chunk in one request and saving it locally.
    But i dont know how to save second chunk along with first. Means append next to previous.

    Please need help. If any body knows please tell me.
    If any can give code snippet then it will be great

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    I believe by saving locally you mean you are writing the byte stream to any file, right? So what is the problem are you facing appending the second byte stream chunk to file?

  3. #3
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Question saving bytes receved in different request

    How can i store it?

    Open file in ByteArrayOutputStream and appending the bytes received in second chunk will create image properly?

    Im not getting how to do that?

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    I think so , appending should work, did u try appending the next chunk to the file.

  5. #5
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Question saving byte r3eceived in diffferent request

    I tried butit wont work.
    Here im pasting my code

    File file = new File(path);
    if (!file.exists()) {
    try {

    file.createNewFile();
    FileOutputStream fos = new FileOutputStream(path);
    fos.write(imarr);
    fos.close();
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } else{
    int len = (int)file.length();
    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(path));
    ByteArrayOutputStream baOutStream = new ByteArrayOutputStream();
    int read = -1;
    byte[] tmpArray = null;
    while(true){
    tmpArray = new byte[CHUNK_SIZE];
    read = fis.read(tmpArray);
    if(read > -1){
    baOutStream.write(tmpArray);
    }else{
    break;
    }
    tmpArray = null;
    }
    tmpArray = null;
    baOutStream.write(imarr);
    tmpArray = baOutStream.toByteArray();
    System.out.println(" tmp array size "+tmpArray.length);
    baOutStream.close();
    file.delete();
    System.out.println(" file exists ");
    FileOutputStream fos = new FileOutputStream(path);
    fos.write(tmpArray);
    fos.close();
    }
    Can you tell me is there anything to change.
    Its not working.? Image is not displaying properly

  6. #6
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default see if it helps

    In my code I am opening a file, reading a chunk of 10 bytes , opening another file in append mode (see the 2nd argument in FileOutputStream as true, its used to open file in append mode, default is false which opens the file in overwrite mode), and appending contents to the file.

    import java.io.*;

    class CopyFileByChunk{
    private static final int CHUNK_SIZE=10;
    public static void main(String args[]){
    try{
    FileInputStream fis=new FileInputStream("c:\\screenShot.jpg");
    byte b[]=new byte[CHUNK_SIZE];

    int read=-1;

    File fl=new File("d:\\dog.jpg");
    if(fl.exists()){
    fl.delete();
    }
    //we open the output file in append mode
    FileOutputStream fos=new FileOutputStream(fl,true);
    while(true){
    read=fis.read(b);
    if(read>-1)
    fos.write(b);
    else
    break;
    }
    }
    catch(Exception e){}
    }

    }

  7. #7
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    I am having a look on your code as well.

  8. #8
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    //considering your code, I think writing this much of code should solve your problem, however I did not have any way to test

    File file = new File(path);
    //if the file does not exist, a new file is created else the file is opened in append mode
    FileOutputStream fos = new FileOutputStream(path,true);
    //I believe imarr is the array which contains the chunk data
    fos.write(imarr);
    fos.close();


    /*one more think which you may have to keep in mind is what happens when two users upload their data from two different mobile devices, the data is appended to the same file, so as per me a possible solution could be you generate the file dynamically i.e. different file name for different users/mobile device. I hope it helps. */

  9. #9
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Smile saving bytes received

    Thanks.
    By using your code i got idea and in last actually you paste code i wrote.

    Thanks

  10. #10
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Question saving bytes received in different request

    Im generating path dynamically according to userId in the request path is generated.

    One more thing when i upload two images for third image server send response as
    HTTP_UNAVAILABLE 503: The server is currently unable to handle the request due to a temporary overloading or maintenance of the server.

    Is there any way to get out of this?

  11. #11
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    You mean your first two images are getting uploaded properly, and the third image is creating problem. Just print some exception stack trace at server and see is it raising any kind of exception, when u try to upload the third image.

  12. #12
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Question saving bytes received in different request

    The thing is when i try to upload data usinglocalhost:8080 i am able to upload many images more than 4.

    When i uplaod the class files to our server and try to upload images then it wont work.

    At server we cant see the what is exception.

    What to do?

  13. #13
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Exclamation saving bytes received in different request

    When im upladoing images, the total request become 80 for 2 images

    But when i uplaod video it was about 200 still at that im not getting 503.

  14. #14
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    As per your comments nothing seems to be wrong with your coding part, because the same code should not behave differently in different machines. But without having a look at the server log/config files its tough to say what exactly is going wrong. Is it happening all the times. What looks to me is it has got something to do with server configuration. Is it happening all the times, I mean is it happening in each time you try to upload the third image? What happens if you close you midlet app and upload image after re starting?

  15. #15
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    It might have got something to do with the gateway as well, but honestly speaking its tough for me confirm from here.

  16. #16
    renuka_anil is offline Member
    Join Date
    Dec 2008
    Posts
    12
    Rep Power
    0

    Question saving bytes received in different request

    Its not happening reguraly.

    Now three images are uploaded. After that i have uploaded 5 images immediately after uplaoding three.

    But before that i have restarted our tomcat server. May because of that they get uploaded. What when there are so many mobiles and so many requests came to server.

    IS there anything that we can do for avoiding situation of overloading?

  17. #17
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Is the tomcat and jre version in both machines same, as u have said u r not facing any problem in localhost.

  18. #18
    wolve634's Avatar
    wolve634 is offline Member
    Join Date
    Oct 2008
    Posts
    13
    Rep Power
    0

    Default

    use a session object!!! :)

  19. #19
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    @renuka_anil
    As per my knowledge tomcat is not very suitable for handling too many requests at a time, and thats why its hardly used in real time scenarios. So I will suggest you to have a look on the servers like JBoss, Websphere etc, because these have a much better memory and thread management techniques. Even if its possible in tomcat , honestly its beyond my knowledge because its totally the server configuration/administration part.

Similar Threads

  1. Replies: 7
    Last Post: 10-28-2008, 01:41 PM
  2. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  3. midi bytes
    By willemjav in forum Advanced Java
    Replies: 77
    Last Post: 07-29-2008, 03:10 PM
  4. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM
  5. how to know the number of bytes
    By gabriel in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 05:13 PM

Posting Permissions

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