2017-04-11 186 views
-1

這是我上傳excel文件在ftp位置上的java代碼。530登錄或密碼不正確

private FTPClient ftp = null; 
    @Value("${excelfile.location}") 
    private String excelFileLocation; 
    @Override 
    public void uploadExcelToFTP() throws Exception { 
     String host = ""; 
     String user = ""; 
     String pass = ""; 
     String hostDir = ""; 
     String localFileFullName = excelFileLocation+"errorSongs-"+dateToString(new Date())+".xlsx"; 
     String fileName = "errorSongs"; 
     ftp = new FTPClient(); 
     ftp.addProtocolCommandListener(new PrintCommandListener(
       new PrintWriter(System.out))); 
     int reply; 
     ftp.connect(host); 
     reply = ftp.getReplyCode(); 
     if (!FTPReply.isPositiveCompletion(reply)) { 
      ftp.disconnect(); 
      throw new Exception("Exception in connecting to FTP Server"); 
     } 
     ftp.login(user, pass); 
     ftp.setFileType(FTP.BINARY_FILE_TYPE); 
     ftp.enterLocalPassiveMode(); 

     File file = new File(localFileFullName); 
     if (file.exists()) { 
      try (InputStream input = new FileInputStream(file)) { 
       this.ftp.storeFile(hostDir + fileName, input); 
      } 
     } 
     /*else{ 
      throw new ExcelFileNotFoundException("File does not exist on location"); 
     }*/ 
     if (this.ftp.isConnected()) { 
      try { 
       this.ftp.logout(); 
       this.ftp.disconnect(); 
      } catch (IOException exception) { 
       // do nothing as file is already saved to server 
      } 
     } 

但我得到以下錯誤:

Command: USER my userid 
Reply: 331 Password required for userid 
Command: PASS **** 
Reply: 530 Login or password incorrect! 

但是,當我嘗試用FileZilla的連接,具有相同的憑證,它的成功連接。

謝謝。

+0

我正在使用java 8。請問這有什麼問題嗎? –

回答

0
String host = ""; 
String user = ""; 
String pass = ""; 
... 
ftp.connect(host); 
ftp.login(user, pass) 

您正在使用空憑證連接到空主機。您應該爲您所需的FTP服務器傳遞有效的主機和證書

+0

我在連接時給予。它被留空以保密。 –

相關問題