Android FTP连接失败

时间:2013-06-19 08:25:28

标签: android ftp android-asynctask

我正在尝试使用FTp连接服务器并下载文件

但是在Android上连接失败,但是当我尝试使用FileZilla或Windows文件资源管理器连接FTP时,它可以工作。背后的原因是什么?

以下是我的代码

class FTPDownload extends AsyncTask<URL , String , Void>{

    boolean running = true;

    Date today = Calendar.getInstance().getTime();    
    Format formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
    String reportDate = formatter.format(today);


    String file = reportDate + "_" + "giant.mp4";

    @Override
    protected Void doInBackground(URL... params) {
        // TODO Auto-generated method stub
        Log.d("******", "Background thread starting......"); 

        FTPClient client = new FTPClient();

        try {
            Log.d("tag" , "arrived");
            client.connect("ftp://newrising.win5.siteonlinetest.com");
            boolean successLogin = client.login("newrising", "newrising2014cap!");

            if(successLogin){
                Log.d("tag" , "success");

                // Get the files stored on FTP Server and store them into an array of FTPFiles
                FTPFile[] files = client.listFiles();
                for (FTPFile ftpFile : files) {
                    // Check the file type and print result
                    if (ftpFile.getType() == FTPFile.FILE_TYPE) {
                        System.out.println("File: " + ftpFile.getName() +
                                "size-> " + FileUtils.byteCountToDisplaySize(
                                        ftpFile.getSize()));
                    }
                }
            }else{
                Log.d("tag" , "sosad");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

以下是我的Logcat报告

06-19 16:22:19.252: W/System.err(15270): java.net.UnknownHostException: Unable to resolve host "ftp://newrising.win5.siteonlinetest.com": Non-recoverable failure in name resolution
06-19 16:22:19.252: W/System.err(15270):    at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
06-19`` 16:22:19.252: W/System.err(15270):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-19 16:22:19.252: W/System.err(15270):    at java.net.InetAddress.getByName(InetAddress.java:295)
06-19 16:22:19.252: W/System.err(15270):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
06-19 16:22:19.252: W/System.err(15270):    at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
06-19 16:22:19.252: W/System.err(15270):    at com.example.recordandmovie.Main$FTPDownload.doInBackground(Main.java:105)
06-19 16:22:19.252: W/System.err(15270):    at com.example.recordandmovie.Main$FTPDownload.doInBackground(Main.java:1)
06-19 16:22:19.252: W/System.err(15270):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-19 16:22:19.252: W/System.err(15270):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-19 16:22:19.252: W/System.err(15270):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-19 16:22:19.252: W/System.err(15270):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-19 16:22:19.252: W/System.err(15270):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-19 16:22:19.252: W/System.err(15270):    at java.lang.Thread.run(Thread.java:856)
06-19 16:22:19.252: W/System.err(15270): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_FAIL (Non-recoverable failure in name resolution)
06-19 16:22:19.262: W/System.err(15270):    at libcore.io.Posix.getaddrinfo(Native Method)
06-19 16:22:19.262: W/System.err(15270):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
06-19 16:22:19.262: W/System.err(15270):    at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
06-19 16:22:19.262: W/System.err(15270):    ... 12 more

1 个答案:

答案 0 :(得分:6)

试试这个:

client.connect("newrising.win5.siteonlinetest.com"); // Remove `ftp://`