FTP下載文件

//須要注意的是下載文件時的編碼格式須要和FTP服務器的編碼格式保持一致。搜索代碼中的 this.ftpClient.setControlEncoding("GBK");
package
net.ssd.publish.web.filter; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.TimeZone; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import org.apache.log4j.Logger; import sun.net.ftp.FtpClient; /** * @ClassName FtpClient * @Description TODO: add type description * @author wangzm * @2013-12-12下午2:28:39 */ public class FtpClient1 { private FTPClient ftpClient; private String strIp; private int intPort; private String user; private String password; private static Logger logger = Logger.getLogger(FtpClient1.class.getName()); /* * * Ftp構造函數 */ public FtpClient1(String strIp, int intPort, String user, String Password) { this.strIp = strIp; this.intPort = intPort; this.user = user; this.password = Password; this.ftpClient = new FTPClient(); } /** * @return 判斷是否登入成功 * */ public boolean connect() { if (this.ftpClient.isConnected()) return true; boolean isLogin = false; FTPClientConfig ftpClientConfig = new FTPClientConfig(); ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID()); // this.ftpClient.setControlEncoding("GBK"); this.ftpClient.setControlEncoding("UTF-8"); this.ftpClient.configure(ftpClientConfig); try { if (this.intPort > 0) { this.ftpClient.connect(this.strIp, this.intPort); } else { this.ftpClient.connect(this.strIp); } // FTP服務器鏈接回答 int reply = this.ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { this.ftpClient.disconnect(); logger.error("登陸FTP服務失敗!"); return isLogin; } isLogin = this.ftpClient.login(this.user, this.password); if(isLogin){ // 設置傳輸協議 this.ftpClient.enterLocalPassiveMode(); this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); logger.info("恭喜" + this.user + "成功登錄FTP服務器"); }else{ throw new RuntimeException("服務器拒絕了鏈接"); } } catch (Exception e) { e.printStackTrace(); logger.error(this.user + "登陸FTP服務失敗!" + e.getMessage()); } this.ftpClient.setBufferSize(1024 * 2); this.ftpClient.setDataTimeout(30 * 1000); return isLogin; } /** * @退出關閉服務器連接 * */ public void disconnect() { if (null != this.ftpClient && this.ftpClient.isConnected()) { try { boolean reuslt = this.ftpClient.logout();// 退出FTP服務器 if (reuslt) { logger.info("成功退出服務器"); } } catch (IOException e) { e.printStackTrace(); logger.warn("退出FTP服務器異常!" + e.getMessage()); } finally { try { this.ftpClient.disconnect();// 關閉FTP服務器的鏈接 } catch (IOException e) { e.printStackTrace(); logger.warn("關閉FTP服務器的鏈接異常!"); } } } } /*** * 上傳Ftp文件 * * @param localFile * 當地文件 * @param romotUpLoadePath上傳服務器路徑 * - 應該以/結束 * */ public boolean uploadFile(File localFile, String romotUpLoadePath) { return this.uploadFile(localFile, romotUpLoadePath, null); } /** * 上傳Ftp文件 * * @param localFile * 本地文件 * @param romoteFileName * 上傳到FTP後的文件名稱 * @param romotUpLoadePath * 上傳到FTP的目錄,若是爲空就是當前目錄 * @return */ public boolean uploadFile(File localFile, String romotUpLoadePath, String romoteFileName) { BufferedInputStream inStream = null; boolean success = false; try { if (romotUpLoadePath != null) { boolean flag = this.ftpClient.changeWorkingDirectory(romotUpLoadePath);// 改變工做路徑 if (!flag) { this.ftpClient.makeDirectory(romotUpLoadePath); this.ftpClient.changeWorkingDirectory(romotUpLoadePath); } } inStream = new BufferedInputStream(new FileInputStream(localFile)); logger.info(localFile.getName() + "開始上傳....."); if (romoteFileName == null) romoteFileName = localFile.getName(); success = this.ftpClient.storeFile(romoteFileName, inStream); if (success == true) { logger.info(localFile.getName() + "上傳成功"); return success; } } catch (FileNotFoundException e) { e.printStackTrace(); logger.error(localFile + "未找到"); } catch (IOException e) { e.printStackTrace(); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return success; } /*** * 下載文件 * * @param remoteFileName * 待下載文件名稱 * @param localDires * 下載到當地那個路徑下 * @param remoteDownLoadPath * remoteFileName所在的路徑 * */ public boolean downloadFile(String remoteFileName, String localDires, String remoteDownLoadPath) { String strFilePath = localDires + remoteFileName; BufferedOutputStream outStream = null; boolean success = false; try { this.ftpClient.changeWorkingDirectory(remoteDownLoadPath); File f = new File(strFilePath); if(!f.getParentFile().exists()) f.getParentFile().mkdirs(); outStream = new BufferedOutputStream(new FileOutputStream(f)); logger.info(remoteFileName + "開始下載...."); success = this.ftpClient.retrieveFile(remoteFileName, outStream); if (success == true) { logger.info(remoteFileName + "成功下載到" + strFilePath); return success; } } catch (Exception e) { e.printStackTrace(); logger.error(remoteFileName + "下載失敗"); } finally { if (null != outStream) { try { outStream.flush(); outStream.close(); } catch (IOException e) { e.printStackTrace(); } } } if (success == false) { logger.error(remoteFileName + "下載失敗!!!"); } return success; } /*** * @上傳文件夾 * @param localDirectory * 當地文件夾 * @param remoteDirectoryPath * Ftp 服務器路徑 以目錄"/"結束 * */ public boolean uploadDirectory(String localDirectory, String remoteDirectoryPath) { File src = new File(localDirectory); try { remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/"; this.ftpClient.makeDirectory(remoteDirectoryPath); // ftpClient.listDirectories(); } catch (IOException e) { e.printStackTrace(); logger.info(remoteDirectoryPath + "目錄建立失敗"); } File[] allFile = src.listFiles(); if (allFile != null) { for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (!allFile[currentFile].isDirectory()) { String srcName = allFile[currentFile].getPath().toString(); uploadFile(new File(srcName), remoteDirectoryPath); } } for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (allFile[currentFile].isDirectory()) { // 遞歸 uploadDirectory(allFile[currentFile].getPath().toString(), remoteDirectoryPath); } } } return true; } /*** * 下載文件夾 * @param localDirectoryPath本地地址 * @param remoteDirectory * 遠程文件夾 * */ public boolean downLoadDirectory(String localDirectoryPath, String remoteDirectory) { try { String fileName = new File(remoteDirectory).getName(); localDirectoryPath = localDirectoryPath + fileName + "//"; new File(localDirectoryPath).mkdirs(); FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory); if (allFile != null) { for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (!allFile[currentFile].isDirectory()) { downloadFile(allFile[currentFile].getName(), localDirectoryPath, remoteDirectory); } } for (int currentFile = 0; currentFile < allFile.length; currentFile++) { if (allFile[currentFile].isDirectory()) { String strremoteDirectoryPath = remoteDirectory + "/" + allFile[currentFile].getName(); downLoadDirectory(localDirectoryPath, strremoteDirectoryPath); } } } } catch (IOException e) { e.printStackTrace(); logger.info("下載文件夾失敗"); return false; } return true; } /** * 重命名遠程文件 * @param name 要更名的原始文件名 * @param newName 新的文件名 * @return * @author wangzm */ public boolean rename(String name, String newName){ boolean r = false; try { r = this.ftpClient.rename(name, newName); } catch (IOException e) { e.printStackTrace(); logger.error("將「"+name+"」重命名爲「"+newName+"」失敗"); } return r; } // FtpClient的Set 和 Get 函數 public FTPClient getFtpClient() { return ftpClient; } @Deprecated public void setFtpClient(FTPClient ftpClient) { this.ftpClient = ftpClient; } public static void main(String[] args) throws IOException { /*FtpClient ftp = new FtpClient("127.0.0.1", 21, "ftp1", "123"); ftp.connect(); // 上傳文件夾 // ftp.uploadDirectory("D:/大連-發佈大風黃色20130923171253.doc", "/2013"); ftp.uploadFile(new File("D:/大連-發佈大風黃色20130923171253.doc"), "/2013/"); // 下載文件夾 // ftp.downLoadDirectory("d://tmp//", "/home/data/DataProtemp"); ftp.disconnect();*/ FtpClient1 t = new FtpClient1("192.168.0.27", 21, "ssdsms", "ssdsms"); t.connect(); t.downloadFile("雲南一鍵發.txt","D:/", "/"); t.disconnect(); // String s ="asdf{^}sa"; // String[] ss = s.split("\\{\\^\\}"); // logger.info(ss[1]); } }
相關文章
相關標籤/搜索