import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import com.eshore.ccc.CCCLogger;
public class FTP {
private String serverIp = "127.0.0.1";
private int port = 21;
private String username ="upload";
private String password = "suntekgc";
private String rootpath = "/";
private String ftptype = "1";
private FTPClient ftpClient;
public FTP() {
}
public FTP(String serverIp, int port, String username, String password, String rootpath, String ftptype) {
this.serverIp = serverIp;
this.port = port;
this.username = username;
this.password = password;
this.rootpath = rootpath;
if(this.rootpath.equals("")) this.rootpath = "/";
this.ftptype = ftptype;
}
private boolean connectFTPServer() {
this.ftpClient = new FTPClient();
try {
//String FTPSERV_TYPE = AppHandle.getHandle("ccf").getProperty("FTPSERV_TYPE", "2");
FTPClientConfig conf = null;
if(this.ftptype.equals("1"))
conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
else
conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpClient.configure(conf);
ftpClient.setDataTimeout(60000);
ftpClient.setControlEncoding("GBK");
ftpClient.setDefaultTimeout(60000);
ftpClient.connect(this.serverIp, port);
boolean b = ftpClient.login(username, password);
if(b)
CCCLogger.logger.info("登錄FTP服務器成功.");
else
CCCLogger.logger.info("登錄FTP服務器失敗.");
return b;
} catch (IOException e) {
CCCLogger.logger.error("登錄FTP服務器失敗", e);
return false;
}
}
public boolean remoteV3ToLocal(String voxFile, String localFile) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
if (!connectFTPServer())
return false;
ftpClient.enterLocalPassiveMode();
ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
// 轉到指定下載目錄
String remoteDir = "/";
String remoteFileName = voxFile;
if (voxFile.contains("/")) {
remoteDir = voxFile.substring(0, voxFile.lastIndexOf("/"));
remoteFileName = voxFile.substring(voxFile.lastIndexOf("/") + 1);
}
if(this.rootpath.endsWith("/"))
remoteDir = this.rootpath + remoteDir;
else
remoteDir = this.rootpath + "/" + remoteDir;
FTPFile file = null;
try {
if(ftpClient.changeWorkingDirectory(remoteDir))
CCFLogger.logger.info("進入FTP目錄:" + remoteDir);
else {
CCFLogger.logger.info("進入FTP目錄:" + remoteDir + "失敗");
return false;
}
FTPFile[] remoteFiles = ftpClient.listFiles();
// 遍歷所有文件,找到指定的文件
for (int i = 0; i < remoteFiles.length; i++) {
if (remoteFiles[i].getName().equals(remoteFileName)) {
file = remoteFiles[i];
CCFLogger.logger.info("從FTP上找到了文件:" + remoteFileName);
break;
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
if(file == null) {
CCFLogger.logger.info("沒有從FTP上找到文件:" + remoteFileName);
return false;
}
File outFile = new File(localFile);
bis = new BufferedInputStream(ftpClient.retrieveFileStream(file.getName()));
bos = new BufferedOutputStream(new FileOutputStream(outFile));
byte[] szBuf = new byte[128 * 1024];
int dwRead = 0;
while (( dwRead = bis.read(szBuf, 0, 128 * 1024)) != -1) {
try {
bos.write(szBuf, 0,dwRead);
bos.flush();
} catch (Exception e) {
}
}
if (ftpClient.isConnected()) {
CCFLogger.logger.info("斷開FTP連接!");
try {
ftpClient.disconnect();
} catch (IOException ioe) {}
}
try {
if (bis != null) bis.close();
if (bos != null) bos.close();
} catch (IOException e) {}
return true;
}
catch (Exception e) {
CCFLogger.logger.error("從FTP下載文件到服務器失敗!", e);
return false;
}
finally {
try {
if (bis != null) bis.close();
if (bos != null) bos.close();
} catch (IOException e) {}
}
}
}
FTP下載文件實例
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.