2013-06-19 112 views
3

我试图使用FTP连接服务器并下载文件Android的FTP连接失败

但连接如在Android的失败,但是当我尝试使用FileZilla中或Windows资源管理器来连接FTP,它作品。背后的原因是什么?下面

是我的代码

类FTPDownload扩展的AsyncTask {

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 

回答

6

试试这个:

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

谢谢!有用! –