Connect server to upload/download by using ftp4j

My machine's charset is traditional chinese(cp950) while server's charset is utf-8, it must be decoded/encoded characters when uploading/downloading.

package ftp;

import it.sauronsoftware.ftp4j.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

public class FileFetch {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		run(args);
		//changeFormat("test.txt");
	}

	private static void changeFormat(String file) throws IOException {
		String tmp = "tmp";
		InputStreamReader reader = new InputStreamReader(new FileInputStream(
				file), "cp936");
		OutputStream ow = new FileOutputStream(tmp);

		char[] buffer = new char[4096];

		int len = 0;
		while (len != -1) {
			len = reader.read(buffer);
			if (len != -1) {
				ow.write(new String(new String(buffer, 0, len)
						.getBytes("cp936")).getBytes("utf-8"));
			}
		}
		reader.close();
		ow.close();

		new File(file).delete();
		new File(tmp).renameTo(new File(file));
	}

	private static void run(String[] args) throws Exception {
		Options options = new Options();
		options.addOption("ip", true, "FTP Server IP");
		options.addOption("u", "user", true, "login user name");
		options.addOption("p", "password", true, "login password");
		options.addOption("rr", "remoateRoot", true,
				"remote file path to be fetch");
		options.addOption("rd", "remoteDir", true,
				"remote file path to be fetch");
		options.addOption("d", "drive", true, "storage file path drive");
		options.addOption("f", "file", true, "storage file path");
		options.addOption("dn", "down", false, "flag to down");
		options.addOption("up", "upload", false, "flag to upload");

		CommandLineParser parser = new PosixParser();
		CommandLine line = parser.parse(options, args);

		String ip = line.getOptionValue("ip");
		String user = line.getOptionValue("user");
		String password = line.getOptionValue("password");
		String remoateRoot = line.getOptionValue("remoateRoot");
		String remoteDir = line.getOptionValue("remoteDir");
		String drive = line.getOptionValue("drive");
		String filePath = line.getOptionValue("file");
		if (line.hasOption("down")) {
			getFiles(ip, user, password, remoateRoot, remoteDir, drive,
					filePath, true);
		} else if (line.hasOption("upload")) {
			getFiles(ip, user, password, remoateRoot, remoteDir, drive,
					filePath, false);
		}

	}

	private static void getFiles(String ip, String user, String passwd,
			String remoateRoot, String remoteDir, String drive,
			String fileName, boolean down) throws Exception {
		FTPClient client = new FTPClient();
		String[] ret = client.connect(ip);
		for (String r : ret) {
			System.out.println("out " + r);
		}

		client.login(user, passwd);

		client.changeDirectory(remoateRoot);
		client.changeDirectory(remoteDir);
		String[] fileNames = client.listNames();
		for (String file : fileNames) {
			if (file.matches(fileName)) {
				String filePath = drive + "/" + remoteDir + "/" + file;
				client.setCharset("UTF-8");
				if (down) {
					File dir = new File(drive + "/" + remoteDir);
					if (!dir.exists()) {
						dir.mkdirs();
					}
					OutputStream f = new FileOutputStream(filePath);
					client.download(file, f, -1, null);
					OutputStreamWriter wr = new OutputStreamWriter(f, "UTF-8");
					wr.flush();
					wr.close();
					changeFormat(filePath);
					System.out.println("file " + filePath + " is downloaded.");

				} else {
					client.upload(new File(filePath));
					System.out.println("file " + filePath + " is uploaded.");
				}
			}
		}
	}
}

ftpfetch.bat
java -cp bin;lib/ftp4j-1.7.jar;lib/commons-cli-1.2.jar ftp.FileFetch -ip 10.13.135.81 -u root -p password -rr /piers/apps/uat/installedApps/XXApps.ear/xx.war -d C:\xx -up -rd . -f pros.*


你可能感兴趣的:(Connect server to upload/download by using ftp4j)