使用apache commons-net包实现文件ftp上传
jopen
11年前
使用apache commons-net包实现文件ftp上传 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.SocketTimeoutException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class FtpTool { private static FTPClient ftp; public static FTPClient ftp_conn(String server, String user, String password) { ftp = new FTPClient(); // ftp.setDefaultTimeout(5000); try { int reply; ftp.connect(server); // ftp.connect(server,21,InetAddress.getLocalHost(),21); System.out.println("Connected to " + server + "."); System.out.println(ftp.getReplyString()); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.out.println("FTP server refused connection."); return null; } else { ftp.login(user, password); System.out.println("Login success."); ftp.pasv(); ftp.enterLocalPassiveMode(); } } catch (SocketTimeoutException ste) { ste.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return ftp; } /** * @param ftp * @param remoteFile * @param localFile * @return * @throws FileNotFoundException */ public static boolean uploadToFtp(FTPClient ftp, String remoteFile, String localFile) throws FileNotFoundException { boolean result = false; if (ftp == null) { return result; } String dir = "/"; remoteFile = remoteFile.replaceAll("\\\\", "/"); if (remoteFile.indexOf("/") != -1) { dir = (String) remoteFile.subSequence(0, remoteFile.lastIndexOf("/")); } FileInputStream fis = new FileInputStream(new File(localFile)); System.out.println("Upload " + localFile + " To " + remoteFile); try { ftp.makeDirectory(dir); ftp.changeWorkingDirectory(dir); ftp.setFileType(FTP.BINARY_FILE_TYPE); // 以BINARY格式传送文件 if (ftp.storeFile(remoteFile, fis)) { result = true; } // System.out.println(ftp.getReplyCode()); fis.close(); } catch (Exception e) { e.printStackTrace(); } return result; } public static void logout(FTPClient ftp) { try { if (ftp != null) { ftp.logout(); ftp.disconnect(); } ftp = null; } catch (IOException e) { e.printStackTrace(); } } } 测试代码; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; public class FtpTest { /** * @param args */ public static void main(String[] args) { String ftp_server = "192.168.0.1"; String ftp_user = "username"; String ftp_password ="password"; FTPClient ftp = FtpTool.ftp_conn(ftp_server, ftp_user, ftp_password); String localFilename="D:\\html\\test.html"; String remoteFilename="/mytest/002/test.html"; System.out.println("upload ..."); System.out.println(localFilename + " to " + remoteFilename); try { FtpTool.uploadToFtp(ftp, remoteFilename, localFilename); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { ftp.logout(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } 相关jar包: apache commons-net-1.4.1.jar