preamble
1, using FTP way upload and download files ( non-SFTP )
2, I hit the hand, pro-test, the code is simple, clear and easy to understand, students in need, please Add conjunction with their actual business logic
2, third party jar package: Import org.apache.commons.net.ftp.FTPClient;
Code
package Main; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Writer; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import Domain.FileInfoList; import Domain.FtpConfig; Util.LoggerUtil Import; * @param password password {class FTPUtils public public static String ftp_IP Final = "192.168.0.6"; public static Integer Final ftp_port = 9001; public static String ftp_user Final = "Administrator"; public static ftp_psw Final String = "123"; public static void main (String args []) { // downloads from the FTP downloadFromFTP (ftp_IP, ftp_port, ftp_user, ftp_psw, "/", "new text document .txt", "E:"); // from the FTP Upload uploadFromFTP (ftp_IP, ftp_port, ftp_user , ftp_psw, "/", " E: // create a new text document .txt", "New text document .txt"); } / ** * @param url IP * @param Port port * @param username Username * @param remotePath server on the path * @param fileName file to be downloaded * @Param localPath saved to the local file * / public static Boolean downloadFromFTP (String URL, int Port, username String, String password, the remotePath String, String fileName, String the localPath) { Boolean = Success to false; FTPClient FTPClient new new FTP = () ; the InputStream INPUT = null; BufferedOutputStream The Writer = null; byte [] by = null; the try { int Reply; ftp.connect (URL, port); // ftp.connect (URL); // if the default port, may be used ftp.connect (url) is directly connected to an FTP server ftp.login (username, password); // login ftp.enterLocalPassiveMode (); ftp.setControlEncoding ( "GBK"); Reply ftp.getReplyCode = (); IF (FTPReply.isPositiveCompletion (Reply)!) { ftp.disconnect (); return Success; } ftp.changeWorkingDirectory (the remotePath); // transferred to the FTP server directory FTPFile [] fs = ftp. listFiles (); // get all the files in the current directory for (FTPFile FF: FS) { IF {(new new String (ff.getName () getBytes (), "UTF-8") the equals (fileName)..) / / Chinese file name appears prevent return null, forcibly set to the ISO-8859-1 coding scheme takes an input stream input = ftp.retrieveFileStream ( "/" + (new String (ff.getName (). getBytes ( "gbk" ), "the ISO-8859-1"))); by new new byte = [(int) ff.getSize ()]; // save the file stream into byte array input.read (by); output file // into target directory, local directory does not need to determine whether there is the same file, it will automatically overwrite Writer BufferedOutputStream the new new = (a FileOutputStream new new (the localPath + "/" + fileName)); writer.Write (by); System.out.println ( "Output File [designated" + fileName + "] to the local directory:" + localPath + "/" + fileName + " success"); } } // FTP connection disconnection ftp.logout (); Success to true = ; } the catch (IOException E) { e.printStackTrace (); } the finally { IF (ftp.isConnected ()) { the try { IF (! Writer = null) { writer.Close (); } IF (! = null INPUT) { input.close (); } ftp.disconnect (); } the catch (IOException IOE) { } } } return Success; } / ** * FTP uploading single files * * @param FTP address ftpIP * @Param port the port number * @param userName ftp user name * @param password ftp password * @param directory to upload to ftp path name does not include the file name * @param localFilePath local file to upload full path name * @param destName to an ftp is stored the file name * @throws IOException * / public static Boolean uploadFromFTP (ftpIP String, Integer Port, the userName String, String password, String Directory, localFilePath String, String destName) { FTPClient new new FTPClient ftpClient = (); FIS = null the FileInputStream; Boolean = Result to false; the try { ftpClient.connect (ftpIP, Port); ftpClient.login (the userName, password); ftpClient.enterLocalPassiveMode (); // set upload directory ftpClient.changeWorkingDirectory (Directory); ftpClient.setBufferSize (1024); ftpClient.setConnectTimeout (10 * 1000); ftpClient.setControlEncoding ( "GBK"); ftpClient.setFileType (FTPClient.BINARY_FILE_TYPE); // prevent the emergence of Chinese file name is returned null, force the file name is set to ISO-8859-1 coding scheme delete a file ftpClient.deleteFile (Directory + new new String (destName.getBytes ( "GBK"), "ISO-8859-1")); file srcFile file new new = (localFilePath); FIS new new = the FileInputStream (srcFile); // uploaded to the FTP server == prevent Chinese file name appears return null, the file name is arranged to force the ISO-8859-1 encoding upload result = ftpClient.storeFile ((directory + new String ( destName.getBytes ( "gbk"), "ISO-8859-1")), fis); } catch (NumberFormatException e) { System.err.println ( "FTP port configuration error: not a number:"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fis); try { ftpClient.disconnect(); } catch (IOException e) { throw new RuntimeException("关闭FTP连接发生异常!", e); } } return result; } }