2012-12-10 156 views
0

我一直在嘗試構建一個應用程序,它捕獲用戶的一些數據項,然後將其全部壓縮到zip文件並通過FTP傳輸到服務器。壓縮部分作爲一種魅力,我相信FTP也會這樣,除了程序拒絕連接到主機。Android FTP拒絕連接主機

我已經嘗試了所有的排列組合和解決方案來解決這個問題,並且一直未能意識到問題所在。我已經包含了apache commons net庫,並且能夠將錯誤指向連接部分。大部分代碼已被註釋掉,只允許使用一個部分。

我的代碼是`package com.example.gis;

import java.io.FileInputStream; 

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

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


public class FTP extends Activity { 

public static FTPClient mFTPClient = null; 
public String TAG="1234"; 
public static String a; 
public static String b; 
//public static int abc=FTP.BINARY_FILE_TYPE; 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.ftp); 

Bundle extras = getIntent().getExtras(); 
boolean m=ftpConnect("ftp://xxxxxxxxxx.xxx", "username omitted", "password omitted", 21); 
if(m==true) 
{ 
    Toast.makeText(this,"FTP Login" , Toast.LENGTH_LONG).show(); 

a=extras.getString("path"); 
b=extras.getString("name"); 
ftpUpload("a", "b", "/test/"); 
ftpDisconnect(); 

} 
else 
    Toast.makeText(this,"Failure to connect" , Toast.LENGTH_LONG).show(); 
} 



public boolean ftpConnect(String host, String username, 
     String password, int port) 
{ 
    mFTPClient=new FTPClient(); 

try { 

// connecting to the host 
mFTPClient.connect(host, port); 

// now check the reply code, if positive mean connection success 
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) { 
// login using username & password 
boolean status = mFTPClient.login(username, password); 
Toast.makeText(this,"Connected as status ="+status, Toast.LENGTH_LONG).show(); 
/* Set File Transfer Mode 
* 
* To avoid corruption issue you must specified a correct 
* transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE, 
* EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE 
* for transferring text, image, and compressed files. 
*/ 
mFTPClient.setFileType(abc); 

mFTPClient.enterLocalPassiveMode(); 

return status; 
} 
} catch(Exception e) { 
Log.d(TAG, "Error: could not connect to host " + host); 
} 

return false; 
} 

public boolean ftpDisconnect() 
{ 
    try { 
     mFTPClient.logout(); 
     mFTPClient.disconnect(); 
     return true; 
    } catch (Exception e) { 
     Log.d(TAG, "Error occurred while disconnecting from ftp server."); 
    } 

    return false; 
} 


/** 
* mFTPClient: FTP client connection object (see FTP connection example) 
* srcFilePath: source file path in sdcard 
* desFileName: file name to be stored in FTP server 
* desDirectory: directory path where the file should be upload to 
*/ 
public boolean ftpUpload(String srcFilePath, String desFileName, 
         String desDirectory) 
{ 
    boolean status = false; 
    try { 
     FileInputStream srcFileStream = new FileInputStream(srcFilePath); 

     // change working directory to the destination directory 
     if (ftpChangeDirectory(desDirectory)) { 
      status = mFTPClient.storeFile(desFileName, srcFileStream); 
     } 

     srcFileStream.close(); 
     return status; 
    } catch (Exception e) { 
     Log.d(TAG, "upload failed"); 
    } 

    return status; 
} 


} 

這段時間以來我一直在嘲笑我。

回答

0
boolean m=ftpConnect("ftp://xxxxxxxxxx.xxx", ... 

您正在使用ftp://指定協議。你不應該那樣做。 您只需指定服務器(或主機),如「ftp.mycompany.com」。

此外,如果你嘗試連接,那麼你必須分別捕獲SocketException,UnknownHostException, 和IOException。通過打印堆棧跟蹤,你會知道 導致了錯誤。我看到你沒有那樣做。

作爲最後一句話,我看到你在主線程上做了這個互聯網訪問。我知道這對ftp來說是可能的(爲什麼對於ftp而不是http?),但是你最好使用一個線程或者一個asynctask。

+0

仍然無法通過將hsot名稱更改爲ftp.xxxxxxxx.net –

+0

thnaks一噸...異步任務工作:) –

相關問題