Discussion Board

Results 1 to 10 of 10

Thread: file upload

  1. #1
    Registered User bejoy_ak's Avatar
    Join Date
    Oct 2009
    Location
    Delhi
    Posts
    124
    hi
    i want to upload a .png file to an ftp server..can anyone send me a sample code for the same...

  2. #2
    Registered User debayandas's Avatar
    Join Date
    Mar 2009
    Posts
    6
    The file upload code can be downloaded from Sony Ericsson website.

    Here's the J2ME client side code -

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.file.*;
    import javax.microedition.io.*;
    import java.io.*;
    
    public class UPLOAD_FileConnection extends MIDlet implements Runnable, CommandListener{
    
        private final String FILE = "/image.jpg";
        private final String URL = "http://localhost/debayan/post.php"; // change this to a valid page.
    
        private final String CrLf = "\r\n";
    
        private Form form = null;
        private Gauge gauge = null;
        private Command exitCommand;
        private Command uploadCommand;
    
        public UPLOAD_FileConnection(){
            form = new Form("Upload File");
            gauge = new Gauge("Progress:", true, 100, 0);
            form.append(gauge);
    
            exitCommand = new Command("Exit", Command.EXIT, 0);
            uploadCommand = new Command("Upload", Command.SCREEN, 0);
            form.addCommand(exitCommand);
            form.addCommand(uploadCommand);
    
            form.setCommandListener(this);
        }
    
        public void startApp() {
            Display.getDisplay(this).setCurrent(form);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
        }
    
        private void progress(int total, int current){
            int percent = (int) (100 * ((float)current/(float)total));
            gauge.setValue(percent);
        }
    
        public void run() {
            httpConn();
        }
    
        private void httpConn(){
            HttpConnection conn = null;
            OutputStream os = null;
            InputStream is = null;
    
            try{
                conn = (HttpConnection)Connector.open(URL);
                conn.setRequestMethod(HttpConnection.POST);
                //InputStream imgIs = getClass().getResourceAsStream("/image.jpg");
    
                FileConnection fconn = (FileConnection)Connector.open("file:///E:/image.jpg", Connector.READ_WRITE);
                InputStream imgIs = fconn.openInputStream();
                //InputStream imgIs = fconn;
    
                byte []imgData = new byte[imgIs.available()];
    
                imgIs.read(imgData);
    
                String message1 = "";
                message1 += "-----------------------------4664151417711" + CrLf;
                message1 += "Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + FILE + "\"" + CrLf;
                message1 += "Content-Type: image/jpg" + CrLf;
                message1 += CrLf;
    
                // the image is sent between the messages ni the multipart message.
    
                String message2 = "";
                message2 += CrLf + "-----------------------------4664151417711--" + CrLf;
    
                conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
                // might not need to specify the content-length when sending chunked data.
                // conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));
    
                System.out.println("open os");
                os = conn.openOutputStream();
    
                System.out.println(message1);
                os.write(message1.getBytes());
    
                // SEND THE IMAGE
                int index = 0;
                int size = 1024;
                do{
                    System.out.println("write:" + index);
                    if((index+size)>imgData.length){
                        size = imgData.length - index;
                    }
                    os.write(imgData, index, size);
                    index+=size;
                    progress(imgData.length, index); // update the progress bar.
    
                }while(index<imgData.length);
                System.out.println("written:" + index);
    
                System.out.println(message2);
                os.write(message2.getBytes());
                os.flush();
    
                System.out.println("open is");
                is = conn.openInputStream();
    
                char buff = 512;
                int len;
                byte []data = new byte[buff];
                do{
                    System.out.println("READ");
                    len = is.read(data);
    
                    if(len > 0){
                        System.out.println(new String(data, 0, len));
                    }
                }while(len>0);
    
                System.out.println("DONE");
    
            }catch(Exception e){
                System.out.println("ERROR" + e);
            }finally{
                System.out.println("Close connection");
                try{
                    os.close();
                }catch(Exception e){}
                try{
                    is.close();
                }catch(Exception e){}
                try{
                    conn.close();
                }catch(Exception e){}
            }
        }
    
        public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
            if(command == exitCommand){
                this.notifyDestroyed();
            }else if(command == uploadCommand){
                new Thread(this).start();
            }
        }
    }
    And here's the PHP server side part -

    Code:
    <?php
    
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    
    ?>

    Find this line of code and change the highlighted text according to your need.

    Code:
    FileConnection fconn = (FileConnection)Connector.open("file:///E:/image.jpg", Connector.READ_WRITE);
    I made some changes to the original code. See if it works for you or download the original.

    Also you may try the following link -

    HTML Code:
    http://wiki.forum.nokia.com/index.php/HTTP_Post_multipart_file_upload_in_Java_ME
    Last edited by debayandas; 2010-03-18 at 05:25.

  3. #3
    Registered User bejoy_ak's Avatar
    Join Date
    Oct 2009
    Location
    Delhi
    Posts
    124
    i want to upload the file to an ftp server...please give me a sample code on that...

  4. #4
    Nokia Developer Champion im2amit's Avatar
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    2,916
    Ref to http://www.forum.nokia.com/forum/showthread.php?t=13914

    Do want to do it S40 phone ? or S60.

    On S40, socket is not available so you cant connect directly to FTP server.

    On S60, you can try porting this: http://www.jibble.org/simpleftp/ to J2me.

    thanks,
    ~Amitabh

  5. #5
    Registered User bejoy_ak's Avatar
    Join Date
    Oct 2009
    Location
    Delhi
    Posts
    124
    Thanks for your reply. What can be done for s40 phone?

  6. #6
    Registered User grahamhughes's Avatar
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,394
    Series 40s (2nd edition and later) support sockets too, depending on network configuration.

    I suggest you seriously consider HTTP rather than FTP (and use the code posted by debayandas), as handsets are more likely to be configured correctly with HTTP support, rather than raw TCP support. If the device is not configured for a TCP connection (or the user's data plan doesn't support it) then SocketConnections won't work.

    Graham.

  7. #7
    Nokia Developer Champion im2amit's Avatar
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    2,916
    For S40, best way is to put a HTTP Proxy server in between which can tansport your file to FTP server.

    From client you can make a multipart HTTP request and transfer all file meta data, FTP creditals and URL to the proxy which can connect to the FTP server on TCP and transfer the file to it.

    Refer to: http://wiki.forum.nokia.com/index.ph...oad_in_Java_ME


    thanks,
    ~Amitabh

  8. #8
    Registered User bejoy_ak's Avatar
    Join Date
    Oct 2009
    Location
    Delhi
    Posts
    124
    Amitabh i tried the simpleFTP given by you but its showing socket class not found....so what should i do to make it working code....also i hve to go for FTP since my client requested that....

  9. #9
    Nokia Developer Champion im2amit's Avatar
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    2,916
    You need to port it to J2me, it doesnt have socket class but can open socket connection using GCF.

    SocketConnection sc = (SocketConnection)
    Connector.open("socket://FTPhost.com:21");


    thanks,
    ~Amitabh

  10. #10
    Registered User bejoy_ak's Avatar
    Join Date
    Oct 2009
    Location
    Delhi
    Posts
    124
    thanks Amitabh for your reply..i will try and let u know the result....

Similar Threads

  1. Replies: 2
    Last Post: 2010-03-05, 01:16
  2. Problems with MBM and MBG file
    By rsiudak in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 5
    Last Post: 2010-02-19, 02:23
  3. Problem with GCCE build in Carbide.vs
    By raffaelbechara in forum Symbian C++
    Replies: 6
    Last Post: 2008-10-20, 16:06
  4. Upload file to server with additional parameter!
    By PopAndDestroy in forum Symbian C++
    Replies: 0
    Last Post: 2008-09-28, 00:04
  5. Unable to upload .csr file on symbian signed
    By jas76 in forum Symbian C++
    Replies: 1
    Last Post: 2008-09-22, 04:12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved