標籤:util username oca logout dir return sage ddr control
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import com.wrt.zjg.webservices.model.FtpModel;
public class FtpUtil {
private static Logger logger = Logger.getLogger(FtpUtil.class);
private static FTPClient ftp;
/**
* 擷取ftp串連
* @param fm
* @return
* @throws Exception
*/
public static boolean connectFtp(FtpModel fm) throws Exception{
ftp = new FTPClient();
boolean flag = false;
int reply;
if(fm.getPort() == null) {
ftp.connect(fm.getIpAddr(), 21);
} else {
ftp.connect(fm.getIpAddr(), fm.getPort());
}
ftp.login(fm.getUserName(), fm.getPwd());
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.changeWorkingDirectory(fm.getPath());
flag = true;
return flag;
}
/**
* 關閉ftp串連
*/
public static void closeFtp() {
if(ftp != null && ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* ftp上傳檔案
* @param file
* @throws Exception
*/
public static void upload(File file) throws Exception {
if(file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for(String fstr : files) {
File file1 = new File(file.getPath() + "/" + fstr);
if(file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "/" + fstr);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
File file2= new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
/**
*
* @param fm 將要上傳的檔案
* @param localBaseDir 本地目錄
* @param remoteBaseDir 遠程目錄
* @throws Exception
*/
public static void startDown(FtpModel fm, String localBaseDir, String remoteBaseDir) throws Exception {
if(FtpUtil.connectFtp(fm)) {
try {
FTPFile[] files = null;
boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir);
if(changedir) {
ftp.setControlEncoding("GBK");
files = ftp.listFiles();
for(int i = 0; i < files.length; i++) {
try {
downloadFile(files[i], localBaseDir, remoteBaseDir);
} catch (Exception e) {
logger.error(e);
logger.error("<"+files[i].getName()+">下載失敗");
}
}
}
} catch (Exception e) {
logger.error(e);
logger.error("下載過程中出現異常!");
}
} else {
logger.error("連結失敗!");
}
}
/**
* 下載FTP檔案
* 當你需要下載FTP檔案的時候,調用此方法
* 根據<b>擷取的檔案名稱,本地地址,遠程地址</b>進行下載
* @param ftpFile
* @param relativeLocalPath
* @param relativeRemotePath
*/
public static void downloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) {
if(ftpFile.isFile()) {
if(ftpFile.getName().indexOf("?") == -1) {
OutputStream outputStream = null;
try {
File locaFile = new File(relativeLocalPath + ftpFile.getName());
if(locaFile.exists()) {
return;
} else {
outputStream = new FileOutputStream(relativeLocalPath + ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), outputStream);
outputStream.flush();
outputStream.close();
}
} catch (Exception e) {
logger.error(e);
} finally {
try {
if(outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
logger.error("輸出檔案流異常");
}
}
}
} else {
String newLocalRelatePath = relativeLocalPath + ftpFile.getName();
String newRemote = new String (relativeLocalPath + ftpFile.getName().toString());
File file = new File(newLocalRelatePath);
if(!file.exists()) {
file.mkdirs();
}
try {
newLocalRelatePath = newLocalRelatePath + "/";
newRemote = newRemote + "/";
String currentWorkDir = ftpFile.getName().toString();
boolean changedir = ftp.changeWorkingDirectory(currentWorkDir);
if(changedir) {
FTPFile[] files = null;
files = ftp.listFiles();
for(int i = 0; i < files.length; i++) {
downloadFile(files[i], newLocalRelatePath, newRemote);
}
}
if(changedir) {
ftp.changeToParentDirectory();
}
} catch (Exception e) {
logger.error(e);
}
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FtpModel fm = new FtpModel();
fm.setIpAddr("192.168.1.252");
fm.setUserName("ftp");
fm.setPwd("ftp12345");
fm.setPath("/temp/zjg/messageXML/");
FtpUtil.connectFtp(fm);
File file = new File("E:/test/959663081-20170606154839294.xml");
FtpUtil.upload(file);
}
}
ftp上傳下載