?? ftpprocess.java
字號:
package com.aspire.xportal.ftp;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPTransferType;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.*;
import com.aspire.xportal.ftp.config.*;
import com.aspire.xportal.ftp.util.*;
public class FtpProcess {
public final static String TRANSFER_ASC = "ASC"; //ascii 傳輸模式
public final static String TRANSFER_BIN = "BIN"; //binary傳輸模式
protected FTPClient ftp;
protected PrintWriter log;
protected FTPConnectMode ftpConnectMode;
public FtpProcess() {
}
/**
*初始化連接,日志等
* @throws IOException
*/
public void initConfig() throws IOException {
initFtpConnectMode();
try {
initLog();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 設置鏈接模式
*/
private void initFtpConnectMode() {
if (FtpConf.getConnectMode().equalsIgnoreCase("active")) {
ftpConnectMode = FTPConnectMode.ACTIVE;
}
else {
ftpConnectMode = FTPConnectMode.PASV;
}
}
/**
* 初始化日志
* @throws IOException
*/
private void initLog() throws IOException {
log = new PrintWriter(new FileWriter(FtpConf.getLocalLog(), true));
}
/**
* ftp連接
* @throws IOException
* @throws FTPException
*/
protected void connect() throws IOException, FTPException {
ftp = new FTPClient(FtpConf.getHost(), FtpConf.getPort());
ftp.debugResponses(true);
ftp.setLogStream(log);
ftp.setConnectMode(ftpConnectMode);
}
/**
* ftp登陸
* @throws IOException
* @throws FTPException
*/
protected void login() throws IOException, FTPException {
ftp.login(FtpConf.getUserName(), FtpConf.getPassword());
setType(TRANSFER_ASC);
}
/**
* 設置傳輸類型
* @param transferType String
* @throws IOException
* @throws FTPException
*/
public void setType(String transferType) throws IOException, FTPException {
if (transferType == null) {
return;
}
if (TRANSFER_ASC.equalsIgnoreCase(transferType)) {
ftp.setType(FTPTransferType.BINARY);
}
if (TRANSFER_BIN.equalsIgnoreCase(transferType)) {
ftp.setType(FTPTransferType.ASCII);
}
}
/**
* 取得遠程目錄中文件列表
* @param remoteDir String 遠程目錄
* @throws IOException
* @throws FTPException
* @return String[]
*/
public String[] getRemoteDirList(String remoteDir) throws IOException,
FTPException {
return ftp.dir(remoteDir);
}
/**
* 取得本地文件列表,包括路徑
* @param localDir String
* @param fileType String 文件類型
* @throws Exception
* @return String[]
*/
public String[] getLocalDirList(String localDir, String fileType) throws
Exception {
return FileUtil.getLocalDirList(localDir, fileType);
}
/**
* 下載遠程目錄指定文件
* @param factLocalDir String 本地文件存放目錄
* @param factRemoteDir String 遠程文件名稱,包括路徑
* @throws Exception
* @return boolean
*/
public boolean transferFile(String factLocalDir, String factRemoteFile) throws
Exception {
try
{
String msg = "";
logg(factRemoteFile);
logg(factLocalDir);
String[] files = getRemoteDirList(factRemoteFile);
if (files.length == 0) {
msg = "文件不存在:" + factRemoteFile;
System.out.println(msg);
return false;
}
mkDir(factLocalDir);
ftp.get(factLocalDir + "/" + getFileNameByPath(factRemoteFile),
factRemoteFile);
logg("傳輸文件:" + factRemoteFile + "成功");
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
/**
* 下載遠程目錄中的所有文件
* @param factLocalDir String 本地文件存放目錄
* @param factRemoteDir String 遠程文件存放目錄
* @throws Exception
* @return boolean
*/
public boolean transferDir(String factLocalDir, String factRemoteDir, String fileType) throws
Exception {
if(fileType == null) fileType = "";
String msg = "";
logg(factRemoteDir);
logg(factLocalDir);
try {
String[] files = getRemoteDirList(factRemoteDir);
if (files.length == 0) {
msg = "文件或目錄不存在:" + factRemoteDir;
logg(msg);
return false;
}
mkDir(factLocalDir);
//ftp.chdir(factRemoteDir);
for (int j = 0; j < files.length; j++) {
logg(files[j]);
String exStr = files[j].substring(files[j].length() -
fileType.length());
if(fileType.equalsIgnoreCase(exStr) || fileType.equals(""))
{
ftp.get(factLocalDir + "/" + getFileNameByPath(files[j]), files[j]);
}
}
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 下載文件
* @param type String 類型(sms,wap,pda)
* @param dateStr String 日期(yyyymm)
* @throws Exception
* @return boolean
*/
public boolean transferText(String type, String dateStr, String fileType) throws Exception {
String msg = "";
String factRemoteDir = "";
String factLocalDir = "";
factRemoteDir = getFactRemoteDir(type, dateStr);
factLocalDir = getFactLocalDir(type, dateStr);
logg(factRemoteDir);
logg(factLocalDir);
return transferDir(factLocalDir, factRemoteDir, fileType);
}
/**
*
* @param type String
* @param dateStr String
* @return String
*/
public String getFactRemoteDir(String type, String dateStr) {
return FtpConf.getFactRemoteDir(type, dateStr);
}
/**
*
* @param type String
* @param dateStr String
* @return String
*/
public String getFactLocalDir(String type, String dateStr) {
return FtpConf.getFactLocalDir(type, dateStr);
}
/**
*
* @param path String
* @return String
*/
private String getFileNameByPath(String path) {
int pos = path.lastIndexOf("/");
return path.substring(pos + 1);
}
/**
* 按日期傳輸指定的文件目錄
* @param dateStr String 日期 yyyymm
* @throws Exception
*/
public void TransferText(String dateStr) throws Exception {
log.println("TransferText()");
setType(this.TRANSFER_ASC);
String[] types = FtpConf.getTypes();
for (int i = 0; i < types.length; i++) {
log.println("TransferType:" + types[i]);
if (transferText(types[i], dateStr, "")) {
String[] list = getRemoteDirList(getFactRemoteDir(types[i],
dateStr));
for (int j = 0; j < list.length; j++) {
ftp.delete(list[j]);
}
ftp.chdir(getFactRemoteDir(types[i], dateStr));
ftp.chdir("..");
ftp.rmdir(getFactRemoteDir(types[i], dateStr));
}
}
}
/**
* 創建目錄
* @param path String
* @throws Exception
* @return boolean
*/
public boolean mkDir(String path) throws Exception {
String msg = null;
java.io.File dir;
dir = new java.io.File(path);
if (dir == null) {
msg = "錯誤原因:對不起,不能創建空目錄!";
throw new Exception(msg);
}
if (dir.isFile()) {
msg = "錯誤原因:已有同名文件" + dir.getAbsolutePath() + "存在。";
throw new Exception(msg);
}
if (!dir.exists()) {
boolean result = dir.mkdirs();
if (result == false) {
msg = "錯誤原因:目錄" + dir.getAbsolutePath() +
"創建失敗,原因不明!";
throw new Exception(msg);
}
return true;
}
// else {
// msg = "錯誤原因:目錄" + dir.getAbsolutePath() + "已存在。";
// }
return true;
}
public void logg(String str) {
System.out.println(str);
}
public void logg(String[] arrayStr) {
for (int i = 0; i < arrayStr.length; i++) {
System.out.println(arrayStr[i]);
}
}
/**
* 退出ftp
*/
public void quit() {
try {
if (ftp != null) {
ftp.quit();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FtpProcess ftp = new FtpProcess();
try {
ftp.initConfig();
ftp.connect();
ftp.login();
//ftp.TransferText("200403");
ftp.logg(ftp.getRemoteDirList("/opt/wap/report/sms/200403"));
ftp.quit();
//String[] arrayStr = ftp.getLocalDirList("e:\\ftp","");
//ftp.logg(arrayStr);
}
catch (IOException ioe) {
ioe.printStackTrace();
ftp.quit();
}
catch (FTPException ftpe) {
ftpe.printStackTrace();
ftp.quit();
}
catch (Exception e) {
e.printStackTrace();
ftp.quit();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -