java的ftp下载目录
长期在aix上看代码,比较痛苦,想当下来回来看或者是备份呢,总是少这少那的,哎,这破系统东西真多,分布的到处都是,想自己搭个环境也总是缺少.h文件, 基于这几年痛苦的代码经历,我就写了这么个类来完成下载我们的代码,不包括执行程序,压缩包,或者是其他什么乱七八糟的东西,真的逼的, 凑活用吧, 后续在完善,
上代码先:
后续需要完善的:
1,写成配置
2,多进程
3,日志记录
4,文件重命名
5,本地压缩
6,优化代码 - 创建文件夹的那段着实写的不好,其他还可以用正则什么的
长期写c的,偶尔java了一把,自我鼓励下:还不赖, 就当v1吧,看先.
将例子写好,加入配置: http://download.csdn.net/detail/zpwmhx/4342943
上代码先:
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("//", "/"); 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; } }我试过了,将系统近600m的代码下载下来, ( 真不知道这么会有这么多垃圾代码 )
后续需要完善的:
1,写成配置
2,多进程
3,日志记录
4,文件重命名
5,本地压缩
6,优化代码 - 创建文件夹的那段着实写的不好,其他还可以用正则什么的
长期写c的,偶尔java了一把,自我鼓励下:还不赖, 就当v1吧,看先.
将例子写好,加入配置: http://download.csdn.net/detail/zpwmhx/4342943