-1

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

    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 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();
            }
        }



    }
}
4

2 回答 2

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();
        }
    }
}
于 2013-10-20T13:34:26.060 回答
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;
    }
  }     
}

希望这个例子对你有帮助

于 2013-10-19T09:01:18.503 回答