java FTPClient 实现文件下载 主机间文件同步

package com.xue;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MyTextUtil {
	public static String ascii2string(String oldText) {
		System.out.println(oldText);
		if (oldText == null || oldText.equals("")) {
			return "";
		}
		String newText = null;
		Character character = (char) Integer.parseInt(oldText.trim());
		newText = character.toString();
		return newText;
	}

	public static String hex2int(String oldNumber) {
		if (oldNumber == null || oldNumber.equals("")) {
			return "";
		}
		return Integer.valueOf(oldNumber, 16).toString();
	}

	public static String hexSequense2String(String sequence, String spilitChar) {
		StringBuffer result = new StringBuffer("");
	String[] split = sequence.split(spilitChar);
		
		for (String string : split) {
			System.out.println(string);
			result.append(ascii2string(hex2int(string)));
		}
		return result.toString();
	}
	
	public static String getStringFromString(String src,String regex,int groupAt){
		 Pattern pattern = Pattern.compile(regex);
		 Matcher matcher =pattern.matcher(src);
		boolean matches = matcher.find();
		if (matches) {
			String group = matcher.group(groupAt);
			return group;
		}else {
			throw new RuntimeException();
		}
	}
	public static String[] getStringFromString(String src,String regex,int[] groupAt){
		 String[] result=new String[groupAt.length];
		 Pattern pattern = Pattern.compile(regex);
		 Matcher matcher =pattern.matcher(src);
		 boolean matches = matcher.find();
		if (matches) {
			for (int i = 0; i < groupAt.length; i++) {
				String group = matcher.group(groupAt[i]);
				result[i]=group;
			}
			return result;
		}else {
			throw new RuntimeException();
		}
	}
	public static String[] getStringFromString(String src,String regex,int[] groupAt, Pattern pattern){
		 String[] result=new String[groupAt.length];
		 Matcher matcher =pattern.matcher(src);
		 boolean matches = matcher.find();
		if (matches) {
			for (int i = 0; i < groupAt.length; i++) {
				String group = matcher.group(groupAt[i]);
				result[i]=group;
			}
			return result;
		}else {
			throw new RuntimeException();
		}
	}
	
	
	public static String getTextFromFile(File file) throws UnsupportedEncodingException {
		try {
			return getTextFromFile(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getTextFromFile(InputStream inputStream) throws UnsupportedEncodingException {
		StringBuffer buffer=new StringBuffer();
	    Scanner scanner=new Scanner(inputStream);
		while (scanner.hasNextLine()) {
			buffer.append(scanner.nextLine()+"\n");
		}
		return new String(buffer.toString().getBytes(),"UTF-8");
	}
	
	public static String getTextFromFile(File file,String CharSet) {
		FileInputStream fileInputStream;
		try {
			fileInputStream = new FileInputStream(file);
		
		byte[] b=new byte[1024];
		byte[] B=new byte[0];
		int read =-1; 
		while ((read=fileInputStream.read(b))>-1) {
			int i=B.length;
			B=Arrays.copyOf(B, B.length+read);
			for(int j=0;j<read;j++){
				B[i+j]=b[j];
			}
		}
		return new String(B,CharSet);
		} catch (FileNotFoundException e) {
			System.out.println(file.getAbsolutePath());
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static String[] getArrayFromString(String bodyText,
			String oneDataRegex) {
		String[] strings=new String[]{};
		java.util.List<String> list=new ArrayList<String>();
		Pattern compile = Pattern.compile(oneDataRegex);
		Matcher matcher = compile.matcher(bodyText);
		while (matcher.find()) {
			String group = matcher.group();
			
			list.add(group);
		}
		return list.toArray(strings);
	}
}

 

package com.xue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import com.xue.FtpMain.Conf;

public class FtpMain {
	private static Conf conf = null;

	/***
	 * 入口方法 该类的作用是利用FTPClient 实现将远程的文件下载到本地目录下, 而且是将远程的目录结构从跟目录开始重建
	 * 比如远程目录是/c/b/d.txt 本地存储根目录是/cd/f/ 则最终拷贝的目录是/cd/f/c/b/d.txt
	 * 
	 * @param args
	 * @throws SocketException
	 * @throws IOException
	 */
	public static void main(String[] args) throws SocketException, IOException {
		conf = Conf.loadConf();// 加载配置文件
		System.out.println(conf);
		FTPClient client = new FTPClient();
		client.connect(conf.getHost(), 21);
		boolean login = client.login(conf.getUsername(), conf.getPass());
		client.enterLocalPassiveMode();// 非常重要 设置成被动模式
		String rootpath = conf.getRootPath();
		client.changeWorkingDirectory(rootpath);
		// 递归远程目录的文件,实现递归式下载
		digui(client, rootpath);
		client.disconnect();
	}

	/***
	 * 递归远程目录,并根据配置实现符合条件的文件名的下载
	 * 
	 * @param client
	 * @param rootpath
	 * @throws IOException
	 */
	private static void digui(FTPClient client, String rootpath) throws IOException {
		client.changeWorkingDirectory(rootpath);
		FTPFile[] listFiles = client.listFiles();
		for (FTPFile ftpFile : listFiles) {
			if (ftpFile.isDirectory()) {
				digui(client, rootpath + "/" + ftpFile.getName());
				client.changeToParentDirectory();// 跳出当前目录 此处非常重要,否则会出现bug
			} else {
				String fileName = ftpFile.getName();
				boolean matches = fileName.matches(conf.getFileameExp());
				if (matches) {
					System.out.println("正在下载文件:" + rootpath + "/" + fileName);
					String filePath = conf.getDesPath() + "/" + rootpath + "/";
					boolean exists = new File(filePath).exists();
					if (!exists) {
						new File(filePath).mkdirs();
					}

					File file = new File(filePath + fileName);
					client.retrieveFile(fileName, new FileOutputStream(file));
					String replyString = client.getReplyString();
					System.out.println(replyString);
					System.out.println("下载完成:" + file.getAbsolutePath());
				}
			}
		}
	}

	/**
	 * 配置加载类
	 * 
	 * @author bobo
	 * 
	 */
	static class Conf {
		private String host;
		private String username;
		private String pass;
		private String rootPath;
		private String soucrePath;
		private String desPath;
		private String fileameExp;
		private Map<String, String> confMap;

		public static Conf loadConf() {
			Conf result = new Conf();
			Map<String, String> map = new HashMap<String, String>();
			try {
				String textFromFile = MyTextUtil.getTextFromFile(new File("conf/conf.conf"));
				String[] split = textFromFile.split("\n");
				for (String string : split) {
					if (string.startsWith("#")) {
						continue;
					}
					String[] split2 = string.split("=");
					map.put(split2[0], split2[1]);
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			result.confMap = map;
			return result;
		}

		public String getHost() {
			return confMap.get("host");
		}

		public String getUsername() {
			return confMap.get("username");
		}

		public String getPass() {
			return confMap.get("pass");
		}

		public String getRootPath() {
			return confMap.get("rootPath");
		}

		public String getSoucrePath() {
			return confMap.get("soucrePath");
		}

		public String getDesPath() {
			return confMap.get("desPath");
		}

		public String getFileameExp() {
			return confMap.get("fileameExp");
		}

		@Override
		public String toString() {
			// TODO Auto-generated method stub
			String result = super.toString();
			;
			result += "\n conf-info:\n";
			Set<String> keySet = this.confMap.keySet();
			for (String key : keySet) {
				String value = this.confMap.get(key);
				result += key + "\t" + value + "\n";
			}
			return result;
		}
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值