java的ftp下載目錄
長期在aix上看代碼,比較痛苦,想當下來回來看或者是備份呢,總是少這少那的,哎,這破系統東西真多,分布的到處都是,想自己搭個環境也總是缺少.h文件, 基于這幾年痛苦的代碼經歷,我就寫了這么個類來完成下載我們的代碼,不包括執行程序,壓縮包,或者是其他什么亂七八糟的東西,真的逼的, 湊活用吧, 后續在完善,
上代碼先:
public class FtpApp {// 需要下載的文件后綴 private static String[] _ext = { ".cp", ".cpp", ".c", ".sh", ".h", ".xml", ".cfg", "makefile" }; // 不需要下載的目錄 private static String[] _not = { "/mgcrm/channel","/mgcrm/work","/mgcrm/run/cfg/commagent/etc","/mgcrm/run/backup", "/mgcrm/work/Python", "/mgcrm/run/cfile/tibss/iasm/src" }; // 本地存儲目錄 private static String local_path = "C:/"; // 遠程下載目錄 private static String path = "/mgcrm/work"; private static String username = "mgcrm"; private static String password = "mgcrm"; private static String HOST = "136.24.224.4"; private static int PORT = 21; // 可以下載的文件列表<全路徑> // static List<String> li_file = new ArrayList<String>(); // 可以下載的目錄<全路徑> // static List<String> li_path = new ArrayList<String>(); private static FtpClient ftpClient = null; /** * connectServer 連接ftp服務器 * @throws java.io.IOException * @param path 文件夾,空代表根目錄 * @param password 密碼 * @param user 登陸用戶 * @param server 服務器地址 */ public void connectServer(String server, int port, String user, String password) throws IOException { // server:FTP服務器的IP地址;user:登錄FTP服務器的用戶名 // password:登錄FTP服務器的用戶名的口令;path:FTP服務器上的路徑 ftpClient = new FtpClient(); ftpClient.openServer(server, port); ftpClient.login(user, password); // 用2進制上傳、下載 ftpClient.binary(); } /** * closeServer 斷開與ftp服務器的鏈接 */ private void closeServer() { try { if (ftpClient != null) { ftpClient.closeServer(); } } catch (IOException e) { e.printStackTrace(); } } /** * 取得某個目錄下的所有文件列表 * @throws IOException */ private List<String> getFileList(String path) { List<String> list = new ArrayList<String>(); BufferedReader dis = null; try { TelnetInputStream nameList = ftpClient.nameList(path); if (nameList != null) { dis = new BufferedReader(new InputStreamReader(nameList)); String filename = ""; while ((filename = dis.readLine()) != null) { list.add(filename); } } } catch (IOException e) { } finally { if (dis != null) { try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } } return list; } /** * 將FTP的文件改名<未使用> */ private boolean rename(String srcFileName, String destFileName) { boolean flag = false; try { ftpClient.rename(srcFileName, destFileName); flag = true; } catch (Exception ex) { ex.printStackTrace(); flag = false; } return flag; } /** * download 從ftp下載文件到本地 * @throws java.lang.Exception * @return long * @param newfilename 本地生成的文件名 * @param filename 服務器上的文件名 */ private long download(String filename, String newfilename) throws Exception { long result = 0; TelnetInputStream is = null; FileOutputStream os = null; try { is = ftpClient.get(filename); java.io.File outfile = new java.io.File(newfilename); os = new FileOutputStream(outfile); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); result = result + c; } outfile = null; } catch (IOException e) { e.printStackTrace(); result = 0; } finally { if (is != null) { is.close(); } if (os != null) { os.close(); } } return result; } /** * @param args */ public static void main(String[] args) { FtpApp dao = new FtpApp(); try { dao.connectServer(HOST, PORT, username, password); dao.getList(path); // dao.downloads(li_file, local_path); } catch (IOException e) { e.printStackTrace(); }finally{ dao.closeServer(); } } private void downloadsOne(String path) { String local_file = null; local_file = local_path + path; createPathifNotExists(local_file); try { if (localFileExists(local_file)) { return; } download(path, local_file); System.out.println(path + " is downloaded :" + local_file); } catch (Exception e) { e.printStackTrace(); } } private boolean localFileExists(String local_file) { return new File(local_file).exists(); } private void downloads(List<String> li_file2, String local_path) { String local_file = null; String remoute_file = null; for (int i = 0; i < li_file2.size(); i++) { remoute_file = li_file2.get(i); local_file = local_path + remoute_file; createPathifNotExists(local_file); try { download(remoute_file, local_file); } catch (Exception e) { e.printStackTrace(); } System.out.println(remoute_file + ", is download: " + local_file); } } private void createPathifNotExists(String local_file) { local_file = local_file.replace("http://", "/"); int c = local_file.lastIndexOf('/'); String fs = local_file.substring(0, c); File f = new File(fs); if (!f.exists()) { f.mkdirs(); } f = null; } private void getList(String path) { List<String> ls = new ArrayList<String>(); String pwd = null; if (path == null || path.trim().equals("")) { return; } try { ftpClient.cd(path); System.out.println("cd " + path); } catch (IOException e) { if (isOkFile(path)) { // / downloadsOne(path); // li_file.add(path); } return; } try { pwd = ftpClient.pwd(); ls = this.getFileList(pwd); } catch (IOException e) { e.printStackTrace(); } if (ls == null || ls.size() < 1) { return; } if (notDownloadDir(pwd)) { return; } // li_path.add(path); if (ls != null) { for (int i = 0; i < ls.size(); i++) { getList(ls.get(i)); } } } private boolean notDownloadDir(String pwd) { for (int i = 0; i < _not.length; i++) { if (pwd.startsWith(_not[i])) { return true; } } return false; } private static boolean isOkFile(String path) { for (int i = 0; i < _ext.length; i++) { if (path.endsWith(_ext[i])) { return true; } } return false; }
}</pre>我試過了,將系統近600m的代碼下載下來, ( 真不知道這么會有這么多垃圾代碼 )
后續需要完善的:
1,寫成配置
2,多進程
3,日志記錄
4,文件重命名
5,本地壓縮
6,優化代碼 - 創建文件夾的那段著實寫的不好,其他還可以用正則什么的
長期寫c的,偶爾java了一把,自我鼓勵下:還不賴, 就當v1吧,看先.
將例子寫好,加入配置: http://download.csdn.net/detail/zpwmhx/4342943
本文由用戶 wmhx 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!