×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Problem httpConnection / FileConnection in download and save file

    Hi all,

    I attemp to download and save the file (any file, in my case, it's HTM file). I have tried it locally, using IIS or APACHE, both are success. However, when i tried downloading from internet, it fails. The file saved with message,"302 Found The file has been moved here". I uploaded the file to a free web hosting. Anyway, i just test it with emulator, havent tested it on actual phone.

    try {
    fc = (FileConnection) Connector.open("file:///root1/index.htm", Connector.READ_WRITE);

    if(!fc.exists()){
    //read the file - convert it to byte[]
    try {
    c = (HttpConnection)Connector.open("http://localhost/index.htm");
    is = c.openInputStream();
    theBytes = new byte[(int)c.getLength()];
    int i;
    while ((i = is.read(theBytes)) != -1) {};
    is.close();
    c.close();

    } catch (IOException ex) {
    ex.printStackTrace();
    }

    //create the file
    fc.create();
    fc.setWritable(true);
    os = fc.openDataOutputStream();
    os.write(theBytes);
    os.flush();
    os.close();
    System.out.println("Success to copy the file");
    fc.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    is it because the web hosting? or is there any method to download and save file to phone?

    Thx in advance......

  2. #2
    Registered User
    Join Date
    Feb 2006
    Location
    Brazil
    Posts
    523

    Re: Problem httpConnection / FileConnection in download and save file

    Quote Originally Posted by wizh View Post
    ...However, when i tried downloading from internet, it fails. The file saved with message,"302 Found The file has been moved here". I uploaded the file to a free web hosting. Anyway, i just test it with emulator, havent tested it on actual phone.

    try {
    fc = (FileConnection) Connector.open("file:///root1/index.htm", Connector.READ_WRITE);

    if(!fc.exists()){
    //read the file - convert it to byte[]
    try {
    c = (HttpConnection)Connector.open("http://localhost/index.htm");
    is = c.openInputStream();
    theBytes = new byte[(int)c.getLength()];
    int i;
    while ((i = is.read(theBytes)) != -1) {};
    is.close();
    c.close();

    } catch (IOException ex) {
    ex.printStackTrace();
    }

    //create the file
    fc.create();
    fc.setWritable(true);
    os = fc.openDataOutputStream();
    os.write(theBytes);
    os.flush();
    os.close();
    System.out.println("Success to copy the file");
    fc.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    is it because the web hosting?...
    If you're trying to retrieve the file from a remote server (the web hosting), you should specify the server's address, and not "localhost":

    Suppose the web hosting address is "www.webhosting.com" .Then use
    Code:
    Connector.open("http://www.webhosting.com/index.htm");
    instead specifying URL as http://localhost/index.htm


    best regards
    Http_user_agent:
    NokiaN80-1/3.0 (4.0707.0.7)
    Series60/3.0
    Profile/MIDP-2.0
    Configuration/CLDC-1.1

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Re: Problem httpConnection / FileConnection in download and save file

    I have tried it. I use the free web hosting, based on PHP and mySQL. But not working ....

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Brazil
    Posts
    523

    Re: Problem httpConnection / FileConnection in download and save file

    The 302 status informs the user-agent the right location of the specified file.
    For example, you want to access the file http://www.somesite.com/file.txt .But let's suppose you type http://somesite.com/file.txt as the address .
    You've missed the www. , but since server admin is aware some people would probably type the server's address without the www. , a redirection is created, and when you try to go to http://somesite.com/file.txt , browser will receive 302 status message, and next, you'll be redirected to the right URL.

    But your application doesn't seem to be handling redirection status.
    You may then find out the exact URL of the file and enter it in your application.


    best regards
    Http_user_agent:
    NokiaN80-1/3.0 (4.0707.0.7)
    Series60/3.0
    Profile/MIDP-2.0
    Configuration/CLDC-1.1

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Re: Problem httpConnection / FileConnection in download and save file

    thanks for the response...
    yes, it will create a file with such html redirect you said.

    but why does it produce such file? remembering i test it locally, it won't do like that, I mean it will retrieve and save the file correctly to the phone emulator memory.

    regards...

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    Brazil
    Posts
    523

    Re: Problem httpConnection / FileConnection in download and save file

    Quote Originally Posted by wizh View Post
    thanks for the response...
    yes, it will create a file with such html redirect you said.

    but why does it produce such file? remembering i test it locally, it won't do like that, I mean it will retrieve and save the file correctly to the phone emulator memory...
    whether the redirection will happen or not depends entirely on the server.
    Once you're trying to retrieve the file from a third party server, it's the server's admin who has control over that.
    But if that's the problem and your application will be using a static URL, you can fix it easily by finding out the correct path of the desired file.


    best regards
    Http_user_agent:
    NokiaN80-1/3.0 (4.0707.0.7)
    Series60/3.0
    Profile/MIDP-2.0
    Configuration/CLDC-1.1

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Re: Problem httpConnection / FileConnection in download and save file

    If that's the case which the server has control, why I can download the file through internet with IE or Firefox. What method actually does the browsers use for downloading the file ?

    regards....

  8. #8
    Registered User
    Join Date
    Feb 2006
    Location
    Brazil
    Posts
    523

    Re: Problem httpConnection / FileConnection in download and save file

    Quote Originally Posted by wizh View Post
    If that's the case which the server has control, why I can download the file through internet with IE or Firefox. What method actually does the browsers use for downloading the file ? ...
    Answer: they handle HTTP status code messages correctly.

    In your J2ME application, you'd need to catch the response from server in order to handle it.

    I'm not sure what phone are you targeted to, but take a look at this thread and see if it can help you:

    http://discussion.forum.nokia.com/fo...ad.php?t=77062


    if it can't (although i wish you good luck, since some phones have a problem in this matter), simply do what was suggested before: find the exact URL of the file (in the case application will always using a permanent URL) and use it in your application.


    best regards
    Http_user_agent:
    NokiaN80-1/3.0 (4.0707.0.7)
    Series60/3.0
    Profile/MIDP-2.0
    Configuration/CLDC-1.1

Similar Threads

  1. How to download and save a zip file
    By netlogins in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2006-06-29, 08:59
  2. File save problem
    By zwgoal in forum Symbian
    Replies: 3
    Last Post: 2005-11-11, 10:11
  3. File Associations and OMA download
    By nicktest in forum Digital Rights Management & Content Downloading
    Replies: 1
    Last Post: 2003-10-21, 15:38

Posting Permissions

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