Commit e4c5cb47 authored by Quxl's avatar Quxl

x

parent c84f699d
package com.egolm.common; package com.egolm.common;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.SocketException; import java.net.SocketException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPSClient; import org.apache.commons.net.ftp.FTPSClient;
/** /**
* FTP工具类 * FTP工具类
* @author 曲欣亮 * @author 曲欣亮
* @since 2015-04-01 * @since 2015-04-01
* *
*/ */
public class FtpUtil { public class FtpUtil {
private static final String fx = ".FTPDOWNLOAD$QUXL@DNE#COM#CN"; private static final String fx = ".FTPDOWNLOAD$QUXL@DNE#COM#CN";
public final FTPClient ftpClient; public final FTPClient ftpClient;
private final String ip; private final String ip;
private final int port; private final int port;
private final String username; private final String username;
private final String password; private final String password;
public static final ExecutorService pool = Executors.newFixedThreadPool(3); public static final ExecutorService pool = Executors.newFixedThreadPool(3);
public static FtpUtil newInstance(String ip, int port, String username, String password, boolean ftps) throws SocketException, IOException { public static FtpUtil newInstance(String ip, int port, String username, String password, boolean ftps) throws SocketException, IOException {
return new FtpUtil(ip, port, username, password, ftps); return new FtpUtil(ip, port, username, password, ftps);
} }
public void disconnect() throws IOException { public void disconnect() throws IOException {
ftpClient.disconnect(); ftpClient.disconnect();
} }
private FtpUtil(String ip, int port, String username, String password, boolean ftps) throws SocketException, IOException { private FtpUtil(String ip, int port, String username, String password, boolean ftps) throws SocketException, IOException {
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
this.username = username; this.username = username;
this.password = password; this.password = password;
if(ftps) { if(ftps) {
this.ftpClient = new FTPSClient(); this.ftpClient = new FTPSClient();
} else { } else {
this.ftpClient = new FTPClient(); this.ftpClient = new FTPClient();
} }
this.ftpClient.connect(ip, port); this.ftpClient.connect(ip, port);
this.ftpClient.login(username, password); this.ftpClient.login(username, password);
this.ftpClient.setControlEncoding("UTF-8"); this.ftpClient.setControlEncoding("UTF-8");
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE); this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
this.ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); this.ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
this.ftpClient.enterLocalPassiveMode(); this.ftpClient.enterLocalPassiveMode();
} }
public void downFolder(final String localRootPath, final String remoteRootPath, boolean async, String backupRootPath) throws IOException { public void downFolder(final String localRootPath, final String remoteRootPath, boolean async, String backupRootPath) throws IOException {
File localFolder = new File(localRootPath); File localFolder = new File(localRootPath);
if(!localFolder.exists()) { if(!localFolder.exists()) {
localFolder.mkdirs(); localFolder.mkdirs();
} }
FTPFile[] listFiles = ftpClient.listFiles(remoteRootPath); FTPFile[] listFiles = ftpClient.listFiles(remoteRootPath);
for(int i = listFiles.length - 1; i >= 0; i--) { for(int i = listFiles.length - 1; i >= 0; i--) {
downFolder(listFiles[i], localRootPath, remoteRootPath, async, backupRootPath); downFolder(listFiles[i], localRootPath, remoteRootPath, async, backupRootPath);
} }
} }
private void downFolder(final FTPFile entry, final String localRootPath, final String remoteRootPath, boolean async, String backupRootPath) throws IOException { private void downFolder(final FTPFile entry, final String localRootPath, final String remoteRootPath, boolean async, String backupRootPath) throws IOException {
String entryName = entry.getName(); String entryName = entry.getName();
String localPath = localRootPath + File.separator + entryName; String localPath = localRootPath + File.separator + entryName;
String remotePath = remoteRootPath + "/" + entryName; String remotePath = remoteRootPath + "/" + entryName;
String backupPath = backupRootPath == null ? null : (backupRootPath + "/" + entryName); String backupPath = backupRootPath == null ? null : (backupRootPath + "/" + entryName);
if(entry.isDirectory()) { if(entry.isDirectory()) {
File file = new File(localPath); File file = new File(localPath);
if(!file.exists()) { if(!file.exists()) {
file.mkdirs(); file.mkdirs();
} }
FTPFile[] listFiles = ftpClient.listFiles(remotePath); FTPFile[] listFiles = ftpClient.listFiles(remotePath);
for(int i = listFiles.length - 1; i >= 0; i--) { for(int i = listFiles.length - 1; i >= 0; i--) {
downFolder(listFiles[i], localPath, remotePath, async, backupPath); downFolder(listFiles[i], localPath, remotePath, async, backupPath);
} }
} else { } else {
downFile(remotePath, localPath, async, backupPath); downFile(remotePath, localPath, async, backupPath);
} }
} }
public void downFile(final String remoteFullName, final String localFullName, boolean async, final String backupFullName) throws FileNotFoundException, IOException { public void downFile(final String remoteFullName, final String localFullName, boolean async, final String backupFullName) throws FileNotFoundException, IOException {
final File localFile = new File(localFullName); final File localFile = new File(localFullName);
if(!localFile.exists() || localFile.isDirectory()) { if(!localFile.exists() || localFile.isDirectory()) {
if(async) { if(async) {
Thread thread = new Thread() { Thread thread = new Thread() {
public void run() { public void run() {
System.out.println("FTPDownload>>Async>>FTP://" + username + "@" + ip + ":" + port + remoteFullName + ">>" + localFullName); System.out.println("FTPDownload>>Async>>FTP://" + username + "@" + ip + ":" + port + remoteFullName + ">>" + localFullName);
String tempFileName = localFullName + fx; String tempFileName = localFullName + fx;
File file = new File(tempFileName); File file = new File(tempFileName);
OutputStream os = null; OutputStream os = null;
FtpUtil ftp = null; FtpUtil ftp = null;
try { try {
os = new FileOutputStream(file); os = new FileOutputStream(file);
ftp = FtpUtil.newInstance(ip, port, username, password, false); ftp = FtpUtil.newInstance(ip, port, username, password, false);
ftp.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftp.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.ftpClient.retrieveFile(remoteFullName, os); ftp.ftpClient.retrieveFile(remoteFullName, os);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { try {
os.close(); os.close();
if(StringUtil.isNotEmpty(backupFullName)) { if(StringUtil.isNotEmpty(backupFullName)) {
String[] names = backupFullName.substring(1).split("/"); String[] names = backupFullName.substring(1).split("/");
String p_name = ""; String p_name = "";
for(int i = 0; i < names.length - 1; i++) { for(int i = 0; i < names.length - 1; i++) {
p_name = p_name + "/" + names[i]; p_name = p_name + "/" + names[i];
try { try {
ftp.ftpClient.makeDirectory(p_name); ftp.ftpClient.makeDirectory(p_name);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); System.out.println(e);
} }
} }
} }
ftp.ftpClient.rename(remoteFullName, backupFullName); ftp.ftpClient.rename(remoteFullName, backupFullName);
ftp.ftpClient.disconnect(); ftp.ftpClient.disconnect();
FileUtil.fileMove(file, localFile); FileUtil.fileMove(file, localFile);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
}; };
pool.execute(thread); pool.execute(thread);
} else { } else {
System.out.println("FTPDownload>>Sync>>" + remoteFullName + ">>" + localFullName); System.out.println("FTPDownload>>Sync>>" + remoteFullName + ">>" + localFullName);
String tempFileName = localFullName + fx; String tempFileName = localFullName + fx;
File file = new File(tempFileName); File file = new File(tempFileName);
ftpClient.retrieveFile(remoteFullName, new FileOutputStream(file)); ftpClient.retrieveFile(remoteFullName, new FileOutputStream(file));
if(StringUtil.isNotEmpty(backupFullName)) { if(StringUtil.isNotEmpty(backupFullName)) {
ftpClient.rename(remoteFullName, backupFullName); ftpClient.rename(remoteFullName, backupFullName);
} }
FileUtil.fileMove(file, new File(localFullName)); FileUtil.fileMove(file, new File(localFullName));
} }
} else { } else {
System.out.println("File exists already: " + localFullName); System.out.println("File exists already: " + localFullName);
} }
} }
public void downFile(final String remoteFullName, final OutputStream os) throws FileNotFoundException, IOException { public void downFile(final String remoteFullName, final OutputStream os) throws FileNotFoundException, IOException {
try { try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFullName, os); ftpClient.retrieveFile(remoteFullName, os);
} finally { } finally {
os.close(); os.close();
ftpClient.disconnect(); ftpClient.disconnect();
} }
} }
public void uploadFile(InputStream is, String remoteFullPath) throws IOException { public void uploadFile(InputStream is, String remoteFullPath) throws IOException {
String[] names = remoteFullPath.substring(1).split("/"); String[] names = remoteFullPath.substring(1).split("/");
String p_name = ""; String p_name = "";
for (int i = 0; i < names.length - 1; i++) { for (int i = 0; i < names.length - 1; i++) {
p_name = p_name + "/" + names[i]; p_name = p_name + "/" + names[i];
try { try {
ftpClient.makeDirectory(p_name); ftpClient.makeDirectory(p_name);
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); System.out.println(e);
} }
} }
ftpClient.storeFile(remoteFullPath, is); ftpClient.storeFile(remoteFullPath, is);
} }
public void uploadFolder(String localFileName, String remoteFullPath) throws IOException { public OutputStream getFileUploadStream(String remoteFullPath) throws IOException {
File localFile = new File(localFileName); String[] names = remoteFullPath.substring(1).split("/");
if(localFile.isFile()) { String p_name = "";
InputStream is = new FileInputStream(localFile); for (int i = 0; i < names.length - 1; i++) {
ftpClient.storeFile(remoteFullPath, is); p_name = p_name + "/" + names[i];
is.close(); try {
} else { ftpClient.makeDirectory(p_name);
String[] names = remoteFullPath.substring(1).split("/"); } catch (Exception e) {
String p_name = ""; System.out.println(e);
for(int i = 0; i < names.length; i++) { }
p_name = p_name + "/" + names[i]; }
try {ftpClient.makeDirectory(p_name);} catch (Exception e) {} return ftpClient.storeUniqueFileStream(remoteFullPath);
} }
ftpClient.changeWorkingDirectory(remoteFullPath);
File[] fs = localFile.listFiles(); public void uploadFolder(String localFileName, String remoteFullPath) throws IOException {
if(fs != null) { File localFile = new File(localFileName);
for(File childFile : fs) { if(localFile.isFile()) {
String childFileFullName = localFile.getAbsolutePath() + File.separator + childFile.getName(); InputStream is = new FileInputStream(localFile);
String remoteChildFullPath = remoteFullPath + "/" + childFile.getName(); ftpClient.storeFile(remoteFullPath, is);
uploadFolder(childFileFullName, remoteChildFullPath); is.close();
} } else {
} String[] names = remoteFullPath.substring(1).split("/");
} String p_name = "";
} for(int i = 0; i < names.length; i++) {
p_name = p_name + "/" + names[i];
public static void main(String[] args) throws Exception { try {ftpClient.makeDirectory(p_name);} catch (Exception e) {}
FtpUtil ftp = FtpUtil.newInstance("10.10.0.67", 21, "egomedia", "egomedia", false); }
//ftp.uploadFolder("D:\\logs\\FH_log", "/"); ftpClient.changeWorkingDirectory(remoteFullPath);
//ftp.uploadFile(new FileInputStream("D:\\logs\\FH_log\\PurePro_"), "/a"); File[] fs = localFile.listFiles();
ftp.downFile("/456/RSAUtil.java", new FileOutputStream(new File("d:/a.java"))); if(fs != null) {
} for(File childFile : fs) {
String childFileFullName = localFile.getAbsolutePath() + File.separator + childFile.getName();
} String remoteChildFullPath = remoteFullPath + "/" + childFile.getName();
uploadFolder(childFileFullName, remoteChildFullPath);
}
}
}
}
public static void main(String[] args) throws Exception {
FtpUtil ftp = FtpUtil.newInstance("10.10.0.67", 21, "egomedia", "egomedia", false);
//ftp.uploadFolder("D:\\logs\\FH_log", "/");
//ftp.uploadFile(new FileInputStream("D:\\logs\\FH_log\\PurePro_"), "/a");
ftp.downFile("/456/RSAUtil.java", new FileOutputStream(new File("d:/a.java")));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment