Commit e4c5cb47 authored by Quxl's avatar Quxl

x

parent c84f699d
package com.egolm.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPSClient;
/**
* FTP工具类
* @author 曲欣亮
* @since 2015-04-01
*
*/
public class FtpUtil {
private static final String fx = ".FTPDOWNLOAD$QUXL@DNE#COM#CN";
public final FTPClient ftpClient;
private final String ip;
private final int port;
private final String username;
private final String password;
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 {
return new FtpUtil(ip, port, username, password, ftps);
}
public void disconnect() throws IOException {
ftpClient.disconnect();
}
private FtpUtil(String ip, int port, String username, String password, boolean ftps) throws SocketException, IOException {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
if(ftps) {
this.ftpClient = new FTPSClient();
} else {
this.ftpClient = new FTPClient();
}
this.ftpClient.connect(ip, port);
this.ftpClient.login(username, password);
this.ftpClient.setControlEncoding("UTF-8");
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
this.ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
this.ftpClient.enterLocalPassiveMode();
}
public void downFolder(final String localRootPath, final String remoteRootPath, boolean async, String backupRootPath) throws IOException {
File localFolder = new File(localRootPath);
if(!localFolder.exists()) {
localFolder.mkdirs();
}
FTPFile[] listFiles = ftpClient.listFiles(remoteRootPath);
for(int i = listFiles.length - 1; i >= 0; i--) {
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 {
String entryName = entry.getName();
String localPath = localRootPath + File.separator + entryName;
String remotePath = remoteRootPath + "/" + entryName;
String backupPath = backupRootPath == null ? null : (backupRootPath + "/" + entryName);
if(entry.isDirectory()) {
File file = new File(localPath);
if(!file.exists()) {
file.mkdirs();
}
FTPFile[] listFiles = ftpClient.listFiles(remotePath);
for(int i = listFiles.length - 1; i >= 0; i--) {
downFolder(listFiles[i], localPath, remotePath, async, backupPath);
}
} else {
downFile(remotePath, localPath, async, backupPath);
}
}
public void downFile(final String remoteFullName, final String localFullName, boolean async, final String backupFullName) throws FileNotFoundException, IOException {
final File localFile = new File(localFullName);
if(!localFile.exists() || localFile.isDirectory()) {
if(async) {
Thread thread = new Thread() {
public void run() {
System.out.println("FTPDownload>>Async>>FTP://" + username + "@" + ip + ":" + port + remoteFullName + ">>" + localFullName);
String tempFileName = localFullName + fx;
File file = new File(tempFileName);
OutputStream os = null;
FtpUtil ftp = null;
try {
os = new FileOutputStream(file);
ftp = FtpUtil.newInstance(ip, port, username, password, false);
ftp.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.ftpClient.retrieveFile(remoteFullName, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
if(StringUtil.isNotEmpty(backupFullName)) {
String[] names = backupFullName.substring(1).split("/");
String p_name = "";
for(int i = 0; i < names.length - 1; i++) {
p_name = p_name + "/" + names[i];
try {
ftp.ftpClient.makeDirectory(p_name);
} catch (Exception e) {
System.out.println(e);
}
}
}
ftp.ftpClient.rename(remoteFullName, backupFullName);
ftp.ftpClient.disconnect();
FileUtil.fileMove(file, localFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
pool.execute(thread);
} else {
System.out.println("FTPDownload>>Sync>>" + remoteFullName + ">>" + localFullName);
String tempFileName = localFullName + fx;
File file = new File(tempFileName);
ftpClient.retrieveFile(remoteFullName, new FileOutputStream(file));
if(StringUtil.isNotEmpty(backupFullName)) {
ftpClient.rename(remoteFullName, backupFullName);
}
FileUtil.fileMove(file, new File(localFullName));
}
} else {
System.out.println("File exists already: " + localFullName);
}
}
public void downFile(final String remoteFullName, final OutputStream os) throws FileNotFoundException, IOException {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFullName, os);
} finally {
os.close();
ftpClient.disconnect();
}
}
public void uploadFile(InputStream is, String remoteFullPath) throws IOException {
String[] names = remoteFullPath.substring(1).split("/");
String p_name = "";
for (int i = 0; i < names.length - 1; i++) {
p_name = p_name + "/" + names[i];
try {
ftpClient.makeDirectory(p_name);
} catch (Exception e) {
System.out.println(e);
}
}
ftpClient.storeFile(remoteFullPath, is);
}
public void uploadFolder(String localFileName, String remoteFullPath) throws IOException {
File localFile = new File(localFileName);
if(localFile.isFile()) {
InputStream is = new FileInputStream(localFile);
ftpClient.storeFile(remoteFullPath, is);
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];
try {ftpClient.makeDirectory(p_name);} catch (Exception e) {}
}
ftpClient.changeWorkingDirectory(remoteFullPath);
File[] fs = localFile.listFiles();
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")));
}
}
package com.egolm.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPSClient;
/**
* FTP工具类
* @author 曲欣亮
* @since 2015-04-01
*
*/
public class FtpUtil {
private static final String fx = ".FTPDOWNLOAD$QUXL@DNE#COM#CN";
public final FTPClient ftpClient;
private final String ip;
private final int port;
private final String username;
private final String password;
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 {
return new FtpUtil(ip, port, username, password, ftps);
}
public void disconnect() throws IOException {
ftpClient.disconnect();
}
private FtpUtil(String ip, int port, String username, String password, boolean ftps) throws SocketException, IOException {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
if(ftps) {
this.ftpClient = new FTPSClient();
} else {
this.ftpClient = new FTPClient();
}
this.ftpClient.connect(ip, port);
this.ftpClient.login(username, password);
this.ftpClient.setControlEncoding("UTF-8");
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
this.ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
this.ftpClient.enterLocalPassiveMode();
}
public void downFolder(final String localRootPath, final String remoteRootPath, boolean async, String backupRootPath) throws IOException {
File localFolder = new File(localRootPath);
if(!localFolder.exists()) {
localFolder.mkdirs();
}
FTPFile[] listFiles = ftpClient.listFiles(remoteRootPath);
for(int i = listFiles.length - 1; i >= 0; i--) {
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 {
String entryName = entry.getName();
String localPath = localRootPath + File.separator + entryName;
String remotePath = remoteRootPath + "/" + entryName;
String backupPath = backupRootPath == null ? null : (backupRootPath + "/" + entryName);
if(entry.isDirectory()) {
File file = new File(localPath);
if(!file.exists()) {
file.mkdirs();
}
FTPFile[] listFiles = ftpClient.listFiles(remotePath);
for(int i = listFiles.length - 1; i >= 0; i--) {
downFolder(listFiles[i], localPath, remotePath, async, backupPath);
}
} else {
downFile(remotePath, localPath, async, backupPath);
}
}
public void downFile(final String remoteFullName, final String localFullName, boolean async, final String backupFullName) throws FileNotFoundException, IOException {
final File localFile = new File(localFullName);
if(!localFile.exists() || localFile.isDirectory()) {
if(async) {
Thread thread = new Thread() {
public void run() {
System.out.println("FTPDownload>>Async>>FTP://" + username + "@" + ip + ":" + port + remoteFullName + ">>" + localFullName);
String tempFileName = localFullName + fx;
File file = new File(tempFileName);
OutputStream os = null;
FtpUtil ftp = null;
try {
os = new FileOutputStream(file);
ftp = FtpUtil.newInstance(ip, port, username, password, false);
ftp.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.ftpClient.retrieveFile(remoteFullName, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
if(StringUtil.isNotEmpty(backupFullName)) {
String[] names = backupFullName.substring(1).split("/");
String p_name = "";
for(int i = 0; i < names.length - 1; i++) {
p_name = p_name + "/" + names[i];
try {
ftp.ftpClient.makeDirectory(p_name);
} catch (Exception e) {
System.out.println(e);
}
}
}
ftp.ftpClient.rename(remoteFullName, backupFullName);
ftp.ftpClient.disconnect();
FileUtil.fileMove(file, localFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
pool.execute(thread);
} else {
System.out.println("FTPDownload>>Sync>>" + remoteFullName + ">>" + localFullName);
String tempFileName = localFullName + fx;
File file = new File(tempFileName);
ftpClient.retrieveFile(remoteFullName, new FileOutputStream(file));
if(StringUtil.isNotEmpty(backupFullName)) {
ftpClient.rename(remoteFullName, backupFullName);
}
FileUtil.fileMove(file, new File(localFullName));
}
} else {
System.out.println("File exists already: " + localFullName);
}
}
public void downFile(final String remoteFullName, final OutputStream os) throws FileNotFoundException, IOException {
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFullName, os);
} finally {
os.close();
ftpClient.disconnect();
}
}
public void uploadFile(InputStream is, String remoteFullPath) throws IOException {
String[] names = remoteFullPath.substring(1).split("/");
String p_name = "";
for (int i = 0; i < names.length - 1; i++) {
p_name = p_name + "/" + names[i];
try {
ftpClient.makeDirectory(p_name);
} catch (Exception e) {
System.out.println(e);
}
}
ftpClient.storeFile(remoteFullPath, is);
}
public OutputStream getFileUploadStream(String remoteFullPath) throws IOException {
String[] names = remoteFullPath.substring(1).split("/");
String p_name = "";
for (int i = 0; i < names.length - 1; i++) {
p_name = p_name + "/" + names[i];
try {
ftpClient.makeDirectory(p_name);
} catch (Exception e) {
System.out.println(e);
}
}
return ftpClient.storeUniqueFileStream(remoteFullPath);
}
public void uploadFolder(String localFileName, String remoteFullPath) throws IOException {
File localFile = new File(localFileName);
if(localFile.isFile()) {
InputStream is = new FileInputStream(localFile);
ftpClient.storeFile(remoteFullPath, is);
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];
try {ftpClient.makeDirectory(p_name);} catch (Exception e) {}
}
ftpClient.changeWorkingDirectory(remoteFullPath);
File[] fs = localFile.listFiles();
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