1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| 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 {
public static void main(String[] args) throws Exception { run(args); }
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."); } } } } }
|