×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    31

    Nokia Asha 302 s40 upload `.txt` file

    I'm trying to upload a text file to my website server and from the emulator works ok, but when I try from my mobile phone i get `42-Error in http` when I print out `e.getMessage()` in the catch block. What could be the problem?

    .java file:
    Code:
    ByteArrayOutputStream bos=null;
    InputStream is=null;
    OutputStream os=null;
    HttpConnection hc=null;
    
    String url="http://{domain}.com/{folder}/index.php";
    String boundary="----------V2ymHFg03ehbqgZCaKO6jy";
    
    Object file=getNote(mainScreen.getSelectedIndex()); //returns a reference to the selected item in a `List` screen
    
    String fileName=(String) file;
    fileName=fileName.substring(0,fileName.length()-4); //removes the `.txt` from the filename
    
    byte[] fileBytes=fileName.getBytes();
    
    StringBuffer sb=new StringBuffer("--").append(boundary).append("\r\n");
    
    sb.append("Content-Disposition: form-data;name=\"").append("file").append("\";filename=\"").append(fileName).append("\"\r\n").append("Content-Type: ").append("text/plain").append("\r\n\r\n");
    
    String boundaryMessage=sb.toString();
    
    String endBoundary="\r\n--"+boundary+"--\r\n";
    
    bos=new ByteArrayOutputStream();
    
    bos.write(boundaryMessage.getBytes());
    
    bos.write(fileBytes);
    
    bos.write(endBoundary.getBytes());
    	
    byte[] postBytes=bos.toByteArray();
    
    bos.close();
    
    hc=(HttpConnection) Connector.open(url,Connector.READ_WRITE);
    
    hc.setRequestMethod(HttpConnection.POST);
    hc.setRequestProperty("User-Agent","HttpMidlet/0.2");
    hc.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
    
    os=hc.openOutputStream();
    
    os.write(postBytes);
    
    os.close();
    hc.close();
    
    int ch;
    
    is = hc.openInputStream();
    
    String res = null;
    
    bos = new ByteArrayOutputStream();
    
    while((ch=is.read())!=-1)
    {
    	bos.write(ch);
    }
    
    res=bos.toString();
    
    is.close();
    bos.close();
    .php file:
    Code:
    $root = "/home/{username}/public_html";
    $dir = "/{folder}/";
    $name = $_FILES[ "file" ][ "name" ];
    $tmp_name = $_FILES[ "file" ][ "tmp_name" ];
    $filesize = filesize( $tmp_name );
    
    echo $root.$dir.$name.".txt";
    
    move_uploaded_file( $tmp_name , $root.$dir.$name.".txt" );

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Nokia Asha 302 s40 upload `.txt` file

    You call hc.openInputStream() after calling hc.close(). Make closing the connection the last thing you do.

    Also: strongly recommend putting InputStream.close(), OutputStream.close() and HttpConnection.close() in finally{} blocks. If you do not close any one of these (because an exception is thrown, perhaps because signal is lost), the connection will never close, and you may eventually choke the device on zombie connections (the app will become unable to open any new connections until restarted).

    Code:
    HttpConnection hc = Connector.open(URL);
    try {
        OutputStream os = hc.openOutputStream();
        try {
            // write output
        } finally {
            os.close();
        }
    
        // ...some processing...
    
        InputStream is = hc.openInputStream();
        try {
            // read input
        } finally {
            is.close();
        }
    } finally {
        hc.close();
    }

Similar Threads

  1. File reading issues in Nokia Asha 303
    By abdul.wahab in forum Mobile Java General
    Replies: 2
    Last Post: 2012-05-24, 11:41
  2. 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
  3. URGENT: File upload problem on Nokia 6680
    By eugeneleck in forum Mobile Java General
    Replies: 4
    Last Post: 2006-02-13, 16:01
  4. XHTML file upload using PHP and a Nokia 7250i
    By EvertRooftop in forum Browsing and Mark-ups
    Replies: 0
    Last Post: 2004-10-27, 02:52

Posting Permissions

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