2013-04-18 66 views
1

我使用ftp4j作爲FTP客戶端。無法使用ftp4j上傳文件

FTPClient client = new FTPClient(); 
client.connect("86.22.11.178"); 
client.login("usr", "pwd"); 
client.changeDirectory("/dir"); 
client.upload(file); 

它在localhost上正常工作,但在封裝在部署在Web服務器上的JSF Web應用程序時無法正常工作。我成功地進行了連接和登錄,當代碼到達上傳命令時,它只是跳過並不做任何事情。沒有例外被拋出。

對於FTP服務器來說,它具有完全的視覺特性,它不是一個問題。我也對這些文件設置了chmod 777權限,並且它們屬於同一個所有者。

這段代碼適用於Windows機器,Linux上運行的機器是否有不同的「規則」?

回答

0

您的代碼似乎是正確的。嘗試找出它拋出的FTP錯誤。有時超時可能發生,我面對!

import org.apache.commons.net.ftp。 ;
import java.io.
;

/** * 這個類是用來演示的Jakarta Commons包網的使用 */
公共類TestFTP {

/** Creates a new instance of TestFTP */ 
public TestFTP() { 
} 

/** 
* main - Unit test program 
* @param args Command line arguments 
* 
*/ 
public static void main(String args[]) { 
    try { 
     String ftpHost = "157.227.38.131"; 
     String ftpUserName = "firebird"; 
     String ftpPassword = "[email protected]"; 
     String ftpRemoteDirectory = "/etc/vlp/uploaded_files"; 
     String fileToTransmit = "c:\\temp\\VLPDYN18022010174439.an"; 

     //Create a Jakarta Commons Net FTP Client object 
     FTPClient ftp = new FTPClient(); 

     //A datatype to store responses from the FTP server 
     int reply; 

     // 
     //Connect to the server 
     // 
     ftp.connect(ftpHost); 

     // 
     // After connection attempt, you should check the reply code to verify success. 
     // 
     reply = ftp.getReplyCode();  
     if(!FTPReply.isPositiveCompletion(reply)) { 
      try { 
       ftp.disconnect(); 
      } catch (Exception e) { 
       System.err.println("Unable to disconnect from FTP server " + 
            "after server refused connection. "+e.toString()); 
      } 
      throw new Exception ("FTP server refused connection."); 
     }     
     System.out.println("Connected to " + ftpHost + ". "+ftp.getReplyString()); 

     // 
     //Try to login 
     // 
     if (!ftp.login(ftpUserName, ftpPassword)) { 
      throw new Exception ("Unable to login to FTP server " + 
           "using username "+ftpUserName+" " + 
           "and password "+ftpPassword); 
     } 

     System.out.println(ftp.getReplyString()); 
     System.out.println("Remote system is " + ftp.getSystemName()); 

     // 
     //Set our file transfer mode to either ASCII or Binary 
     // 
     //ftp.setFileType(FTP.ASCII_FILE_TYPE); 
     ftp.setFileType(FTP.BINARY_FILE_TYPE); 

     // 
     //Change the remote directory 
     // 
     if (ftpRemoteDirectory != null && ftpRemoteDirectory.trim().length() > 0) { 
      System.out.println("Changing to FTP remote dir: " + ftpRemoteDirectory); 
      ftp.changeWorkingDirectory(ftpRemoteDirectory); 
      reply = ftp.getReplyCode(); 

      if(!FTPReply.isPositiveCompletion(reply)) { 
       throw new Exception ("Unable to change working directory " + 
            "to:"+ftpRemoteDirectory); 
      } 
     } 

     // 
     //Get the file that we will transfer and send it. 
     // 
     File f = new File(fileToTransmit); 
     System.out.println("Storing file as remote filename: " + f.getName()); 
     boolean retValue=true; 
     try{ 
     retValue = ftp.storeFile(f.getName(), new FileInputStream(f)); 
     }catch(Exception e){e.printStackTrace();} 
     if (!retValue) { 
      throw new Exception ("Storing of remote file failed. ftp.storeFile() returned false."); 
     } 

     //Disconnect from the FTP server 
     // 
     try { 
      //ftp.logout(); 
      ftp.disconnect(); 
     } catch (Exception exc) { 
      System.err.println("Unable to disconnect from FTP server. " + exc.toString()); 
     } 

    } catch (Exception e) { 
     System.err.println("Error: "+e.toString()); 
    } 

    System.out.println("Process Complete."); 
    System.exit(0); 
}  

}

+0

我怎麼能找到FTP錯誤?我無法找到它,那就是問題! – Shani

+0

@Shani你是否試過。你解決了你的問題嗎? – Hariharan