Java中apache下面FTPClient主動模式和被動模式
阿新 • • 發佈:2019-02-20
再仔細測試了一下,果然問題就出在這裡。解決了。此時正確的程式碼為
同理,我們在下載對應的結果檔案的時候。也需要設定相應的被動模式。/** * 被動模式---上傳檔案 * @param ip * @param port * @param userName * @param password * @param sourceStream * @param ftpDir * @param ftpFileName * @param charset * @throws FrontEndException */ public final static void uploadPassiveMode(String ip, int port, String userName, String password, InputStream sourceStream, String ftpDir, String ftpFileName, String charset) throws FrontEndException { FTPClient ftpClient = new FTPClient(); try { if (port <= 0) ftpClient.connect(ip); else ftpClient.connect(ip, port); //登入 boolean login = ftpClient.login(userName, password); StringBuilder service = new StringBuilder(); service.append("上傳FTP【ip="+ip+",port="+port+"】,"); if(login){ LOGGER.info(service.append("登入成功...").toString()); //設定上傳目錄 ftpClient.changeWorkingDirectory(ftpDir); ftpClient.setBufferSize(1024); ftpClient.setControlEncoding(charset); //設定被動模式 <span style="color:#ff0000;">ftpClient.enterLocalPassiveMode();</span> //設定檔案型別(二進位制) ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); if(ftpClient.storeFile(ftpFileName, sourceStream)){ LOGGER.info(service.append("上傳成功...").toString()); } else { LOGGER.info(service.append("上傳失敗...").toString()); } } else { LOGGER.info(service.append("登入失敗...").toString()); } } catch (IOException e) { throw new FrontEndException("上傳檔案失敗", e); } finally { try { ftpClient.disconnect(); } catch (IOException e) { LOGGER.error("斷開ftp連線異常", e); } } }