将文件夹上传到FTP服务器

时间:2013-10-19 08:50:42

标签: java android ftp ftp-client

我在这里搜索了这个主题,我尝试了所有我找到的,但它仍然不起作用。

    import java.io.FileInputStream;
    import java.io.IOException;

    import org.apache.commons.net.ftp.FTPClient;

    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;


    public class MainActivity extends Activity {

        public static final String TAG = "Contacts";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

我创建了一个Thread,因为它不允许在主线程上运行它;

            Thread t = new Thread(new Runnable(){
                @Override
                public void run(){
                    Versuch();
                }
            });
            t.start();
    }

在这里,我尝试上传数据,并没有在Log Cat中显示错误。

public void Versuch(){
        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {
            client.connect("ftp-web.example");
            client.login("ftpuser", "ftppassword");


            String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM";
            fis = new FileInputStream(filename);

            // Store file to server
            //
            client.storeFile(filename, fis);
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }
}

2 个答案:

答案 0 :(得分:1)

由于您没有收到任何错误,请尝试使用适用于我的示例。

之前,

  • 检查您的服务器是否启用了FTP。
  • 确保该设备与服务器具有相同的子网
  • (最佳)运行FTP上传时,使用access_log检查服务器端error_log / tail。验证服务器是否侦听并获取某些内容

我使用commons-net-3.1.jar

以下是我们上传bin文件的方法:

public void uploadFileToServer(
                            String serverIP,
                            String binFileToStore,
                            String workingRemoteFolder,
                            String localFilePath,
                            String timeout
                            ){
Log.d("test", "init FTP client ...");


// set FTP client
FTPClient client = new FTPClient();
client.setConnectTimeout(Integer.parseInt(timeout)*1000);
client.setDefaultTimeout(Integer.parseInt(timeout)*1000);
client.setControlKeepAliveTimeout(Long.parseLong(timeout) );


try{
    int reply;

    Log.d("test", "connecting ...");


    client.connect(serverIP);
    // After connection attempt, you should check the reply code to verify
    // success.
    reply = client.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply))
    {
        client.disconnect();

        //error

        return;
    }
}
catch (IOException e){

    return;
}

/** set setup configuration. We upload bin file */ 
FileInputStream fis = null;

try{

    client.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    client.enterLocalPassiveMode();

    client.login("automation", "automation");

    //
    // Create an InputStream of the file to be uploaded
    //
    fis = new FileInputStream(localFilePath);


    if (!client.changeWorkingDirectory(workingRemoteFolder)){
        client.makeDirectory(workingRemoteFolder);
        client.changeWorkingDirectory(workingRemoteFolder);
    }

    Log.d("test", "store file ...");

    boolean result = client.storeFile(binFileToStore, fis);

    // done

    client.logout();

} catch (IOException e) {

} finally {
    try {
        if (fis != null) {
            fis.close();
        }
        client.disconnect();
    } catch (IOException e) {

        return;
    }
  }     
}

希望这个例子可以帮到你

答案 1 :(得分:1)

这是我的问题的解决方案:我忘了说正确的FileType和TransferMode。并且很重要:登录后设置FileType和TransferMode。

以下是整个更正后的代码:

package com.example.upload_contacts;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import android.os.Bundle;
import android.util.Log;
import android.app.Activity;


public class MainActivity extends Activity {

    public static final String TAG = "Contacts";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread t = new Thread(new Runnable(){
            @Override
            public void run(){
                jetzt();
            }
        });
        t.start();
        Log.i(TAG, "thread started");

    }

    public void jetzt(){
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect("YourHostHere");

            ftpClient.setSoTimeout(10000);
            ftpClient.enterLocalPassiveMode();
            if(ftpClient.login("YourUserHere", "YourPassHere"))
            {
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
                File sFile=new File("mnt/sdcard/DCIM/komik.jpg");
                FileInputStream fs= new FileInputStream(sFile);
                String fileName = sFile.getName();
                Boolean result = ftpClient.storeFile(fileName, fs);
                fs.close();
                Log.i(TAG, "sent");
                String has = "";
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}