Uploading a file to a FTP server from android phone?

前端 未结 3 934
终归单人心
终归单人心 2020-12-28 09:18

Following is the code that\'s suppose to create a text document and upload it to my FTP server. For some reason it doesn\'t seem to work. I used to the libraries provided at

3条回答
  •  余生分开走
    2020-12-28 09:59

    See this...... This will help you rectify the probs in your code.

    I have used the apache's commons library to upload and download an Audio file to and from the Server... see this...

    Uploading:

    public void goforIt(){
    
    
            FTPClient con = null;
    
            try
            {
                con = new FTPClient();
                con.connect("192.168.2.57");
    
                if (con.login("Administrator", "KUjWbk"))
                {
                    con.enterLocalPassiveMode(); // important!
                    con.setFileType(FTP.BINARY_FILE_TYPE);
                    String data = "/sdcard/vivekm4a.m4a";
    
                    FileInputStream in = new FileInputStream(new File(data));
                    boolean result = con.storeFile("/vivekm4a.m4a", in);
                    in.close();
                    if (result) Log.v("upload result", "succeeded");
                    con.logout();
                    con.disconnect();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
    
    
    
    
    
        }
    

    Downloading:

    public void goforIt(){
        FTPClient con = null;
    
        try
        {
            con = new FTPClient();
            con.connect("192.168.2.57");
    
            if (con.login("Administrator", "KUjWbk"))
            {
                con.enterLocalPassiveMode(); // important!
                con.setFileType(FTP.BINARY_FILE_TYPE);
                String data = "/sdcard/vivekm4a.m4a";
    
                OutputStream out = new FileOutputStream(new File(data));
                boolean result = con.retrieveFile("vivekm4a.m4a", out);
                out.close();
                if (result) Log.v("download result", "succeeded");
                con.logout();
                con.disconnect();
            }
        }
        catch (Exception e)
        {
            Log.v("download result","failed");
            e.printStackTrace();
        }
    
    
    
    }
    

提交回复
热议问题