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
謝謝!有用! –