package xxxx; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FtpUtil { private final static String host = "192.168.xxx.xxxx"; private final static String basePath = "/auth"; private final static String username = ""; private final static String password = ""; public static boolean uploadFile(String localPath) throws IOException { File file = new File(localPath); FileInputStream fileInputStream = null; boolean result = false; FTPClient ftp = new FTPClient(); try { //---------------------- //嘗試添加超時時間 解決阻塞問題 ftp.setDefaultTimeout(60 * 1000); ftp.setConnectTimeout(60 * 1000); ftp.setDataTimeout(60 * 1000); //---------------------- fileInputStream = new FileInputStream(file); String fileName = file.getName(); int reply; ftp.connect(host);// 鏈接FTP服務器 ftp.login(username, password);// 登陸 reply = ftp.getReplyCode(); //230表明登陸成功 System.out.println(reply); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(basePath); ftp.setFileType(FTP.BINARY_FILE_TYPE); //ftp.setBufferSize(1024); ftp.setControlEncoding("GBK"); //設置被動模式 ftp.enterLocalPassiveMode(); //設置上傳文件的類型爲二進制類型 //上傳文件 result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream); fileInputStream.close(); ftp.logout(); // result = true; } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } public static boolean uploadFile(File file) throws IOException { FileInputStream fileInputStream = null; boolean result = false; FTPClient ftp = new FTPClient(); try { //---------------------- //嘗試添加超時時間 解決阻塞問題 ftp.setDefaultTimeout(60 * 1000); ftp.setConnectTimeout(60 * 1000); ftp.setDataTimeout(60 * 1000); //---------------------- fileInputStream = new FileInputStream(file); String fileName = file.getName(); int reply; ftp.connect(host);// 鏈接FTP服務器 ftp.login(username, password);// 登陸 reply = ftp.getReplyCode(); //230表明登陸成功 System.out.println(reply); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(basePath); ftp.setFileType(FTP.BINARY_FILE_TYPE); //ftp.setBufferSize(1024); ftp.setControlEncoding("GBK"); //設置被動模式 ftp.enterLocalPassiveMode(); //設置上傳文件的類型爲二進制類型 //上傳文件 result = ftp.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fileInputStream); fileInputStream.close(); ftp.logout(); // result = true; } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } public static boolean downloadFile(String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host); // 若是採用默認端口,可使用ftp.connect(host)的方式直接鏈接FTP服務器 ftp.login(username, password);// 登陸 reply = ftp.getReplyCode(); System.out.println(reply); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath);// 轉移到FTP服務器目錄 ftp.enterLocalPassiveMode(); FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /*public static void main(String[] args) { String localpath = "D:/ftp測試.txt"; System.out.println(uploadFile(localpath)); System.out.println(downloadFile("/auth", "ftp測試.txt", "D:/log")); }*/ }
使用的jar包java
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>2.0</version> </dependency>