×

Discussion Board

Results 1 to 4 of 4

Thread: File Upload

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Mumbai, India
    Posts
    54

    File Upload

    Hi,
    I am trying to upload files from a mobile device. The code is as under:

    FileConnection fc = (FileConnection)Connector.open("file://localhost/" + path);

    if(!fc.exists()) {
    fc.create();
    }
    fc.setReadable(true);

    InputStream input = fc.openInputStream();

    //Creates an input stream from File Connection.
    HttpConnection hc = null;

    InputStream is = null;

    hc = (HttpConnection) Connector.open(url);

    OutputStream dout = hc.openOutputStream();

    int read;

    byte[] buffer = new byte[1024];

    // reading from the input stream and writing to the output stream.

    while ((read = input.read(buffer,0,1024)) != -1) {

    System.out.println("writing");

    dout.write(buffer, 0, read);
    }


    Now, the above code works very well with the emulator. It could upload files upto 1 mb.

    However, when i transfered it to N95, and tried connecting over the operator's gateway, the request reached the server, but the server could not read any bytes of data from the ServletInputStream.

    Now, when i connected the n95 over my wireless LAN, the code worked just fine.

    Here is the server code for reading the Streams:


    System.out.println("invoked");



    try
    {

    ServletInputStream sis = request.getInputStream();

    int l;

    byte abyte1[] = new byte[512];
    ByteArrayOutputStream binArray = new ByteArrayOutputStream();
    while((l = sis.read(abyte1)) != -1)
    {

    System.out.println("Reading Servlet input stream"+l);

    binArray.write(abyte1, 0, l);
    }



    String path = AppConstants.LOCAL_CONTEXT_PATH+"//upldDOC001.mp3";
    OutputStream targetFile = new FileOutputStream(path);



    //System.out.println(new String(binArray.toByteArray()));

    targetFile.write(binArray.toByteArray());


    targetFile.close();

    System.out.println("File written");

    }catch(Exception e)
    {
    e.printStackTrace();
    }


    Again, if some one has a robust code to upload files without causing an out of memory, please help !!

    Thanks,
    Sanket

  2. #2
    Regular Contributor
    Join Date
    Jul 2003
    Posts
    89

    Re: File Upload

    make sure that your server's IP address is public. Since you are able to reach it via WLAN and not via Operator gateway.

    Are you sure you are able to read the file bytes into the stream. I have not come across file://localhost....before but since you are able to read the file then I have to take your word for it.

    More on local file access on Nokia devices can be found at
    http://www.forum.nokia.com/document/...5-94479F5BE26E

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Mumbai, India
    Posts
    54

    Re: File Upload

    Hi Morash,
    Thanks for your inputs. The IP is public, in both the cases, WLAN and the Operator gateway. Again, I am sure that I am able to read the file, since, I was using the same code earlier, where in I was first reading the file into a BufferedByteArray, and then writing the BufferedByteArray to the output stream, and it worked. I had to discontinue this since it gave me an out of memory for larger files. So, I am reading the file bytes, and directly writing it out to the output stream. But this is not working on the phone, but works on the emulator and the n95 through a WLAN connection.
    Also, is there anything that I am missing here, like specifying the http header's etc?

  4. #4
    Regular Contributor
    Join Date
    Jul 2003
    Posts
    89

    Re: File Upload

    I thought so but I figured it is better to double check that.

    Flush your stream.
    Instead of reading the entire file read chunks of it into the stream and upload, on next read simply skip the bytes that you have already read and upload another chunk.

    Check in your N95's conneciton manager to see as to how much it transfers before giving an error.

    I cannot see any headers being set in your code snippet so again I am assuming that you are setting all the necessary headers.
    There are a few examples on FN Wiki. Try these or post the code.

    http://wiki.forum.nokia.com/index.ph...E_%26_servlets

    http://wiki.forum.nokia.com/index.ph...load_with_J2ME

Similar Threads

  1. Multipart File Upload
    By jini_1 in forum Mobile Java General
    Replies: 2
    Last Post: 2009-10-14, 12:35
  2. File upload .....HTTP /Post
    By Mdhaval in forum Symbian
    Replies: 2
    Last Post: 2008-04-12, 15:45
  3. Upload the file
    By shashidon in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2008-04-09, 05:49
  4. Do u think Nokia 3G phone support File Upload???
    By DancingWave in forum Browsing and Mark-ups
    Replies: 9
    Last Post: 2007-03-15, 09:30
  5. Series 60 SDK Appwizard on windows xp
    By moonjoor in forum Symbian Tools & SDKs
    Replies: 15
    Last Post: 2003-12-24, 10:22

Posting Permissions

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