最近因工作需要,数据库中的数据需要从FTP服务中抽取数据文件然后校检再抽取到数据中。因为第一步需要从FTP服务中抽取数据文件。第二步采用JDBC批量数据更新。
1。采用Apache.FTPClient:
/
* Apache.FTPClient FTP操作共公类
*
* @author 张明学
*
*/
public class FTPCommon {
private FTPClient ftpClient;
private FTPModel ftpModel;
public FTPCommon(FTPModel ftp) {
super();
// 从配置文件中读取初始化信息
this.ftpClient = new FTPClient();
this.ftpModel = ftp;
}
/
* 连接并登录FTP服务器
*
*/
public boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig(
FTPClientConfig.SYST_NT);
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(ftpClientConfig);
try {
if (this.ftpModel.getPort() > 0) {
this.ftpClient.connect(ftpModel.getUrl(), ftpModel.getPort());
} else {
this.ftpClient.connect(ftpModel.getUrl());
}
// FTP服务器连接回答
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
return isLogin;
}
this.ftpClient.login(this.ftpModel.getUsername(), this.ftpModel
.getPassword());
this.ftpClient.changeWorkingDirectory(this.ftpModel.getRemoteDir());
this.ftpClient.setFileType(FTPClient.FILE_STRUCTURE);
LogUtil.infoOutPut("成功登陆FTP服务器:" + this.ftpModel.getUrl() + " 端口号:"
+ this.getFtpModel().getPort() + " 目录:"
+ this.ftpModel.getRemoteDir());
isLogin = true;
} catch (SocketException e) {
e.printStackTrace();
LogUtil.logPrint("连接FTP服务失败!", Constants.LOG_EXCEPTION);
LogUtil.logPrint(e.getMessage(), Constants.LOG_EXCEPTION);
} catch (IOException e) {
e.printStackTrace();
LogUtil.logPrint("登录FTP服务失败!", Constants.LOG_EXCEPTION);
LogUtil.logPrint(e.getMessage(), Constants.LOG_EXCEPTION);
}
System.out.println(this.ftpClient.getBufferSize());
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(2000);
return isLogin;
}
/
* 退出并关闭FTP连接
*
*/
public void close() {
if (null != this.ftpClient && this.ftpClient.isConnected()) {
try {
boolean reuslt = this.ftpClient.logout();// 退出FTP服务器
if (reuslt) {
LogUtil.info("退出并关闭FTP服务器的连接");
}
} catch (IOException e) {
e.printStackTrace();
LogUtil.exception("退出FTP服务器异常!");
LogUtil.exception(e.getMessage());
} finally {
try {
this.ftpClient.disconnect();// 关闭FTP服务器的连接
} catch (IOException e) {
e.printStackTrace();
LogUtil.exception("关闭FTP服务器的连接异常!");
LogUtil.exception(e.getMessage());
}
}
}
}
/
* 检查FTP服务器是否关闭 ,如果关闭接则连接登录FTP
*
* @return
*/
public boolean isOpenFTPConnection() {
boolean isOpen = false;
if (null == this.ftpClient) {
return false;
}
try {
// 没有连接
if (!this.ftpClient.isConnected()) {
isOpen = this.ftpLogin();
}
} catch (Exception e) {
e.printStackTrace();
LogUtil.exception("FTP服务器连接登录异常!");
LogUtil.exception(e.getMessage());
isOpen = false;
}
return isOpen;
}
/
* 设置传输文件的类型[文本文件或者二进制文件]
*
* @param fileType--FTPClient.BINARY_FILE_TYPE,FTPClient.ASCII_FILE_TYPE
*/
public void setFileType(int fileType) {
try {
this.ftpClient.setFileType(fileType);
} catch (IOException e) {
e.printStackTrace();
LogUtil.exception("设置传输文件的类型异常!");
LogUtil.exception(e.getMessage());
}
}
/
* 下载文件
*
* @param localFilePath
* 本地文件名及路径
* @param remoteFileName
* 远程文件名称
* @return
*/
public boolean downloadFile(String localFilePath, String remoteFileName) {
BufferedOutputStream outStream = null;
boolean success = false;
try {
outStream = new BufferedOutputStream(new FileOutputStream(
localFilePath));
success = this.ftpClient.retrieveFile(remoteFileName, outStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outStream != null) {
try {
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
/
* 下载文件
*
* @param localFilePath
* 本地文件
* @param remoteFileName
* 远程文件名称
* @return
*/
public boolean downloadFile(File localFile, String remoteFileName) {
BufferedOutputStream outStream = null;
FileOutputStream outStr = null;
boolean success = false;
try {
outStr = new FileOutputStream(localFile);
outStream = new BufferedOutputStream(outStr);
success = this.ftpClient.retrieveFile(remoteFileName, outStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != outStream) {
try {
outStream.flush();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != outStr) {
try {
outStr.flush();
outStr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return success;
}
/
* 上传文件
*
* @param localFilePath
* 本地文件路径及名称
* @param remoteFileName
* FTP 服务器文件名称
* @return
*/
public boolean uploadFile(String localFilePath, String remoteFileName) {
BufferedInputStream inStream = null;
boolean success = false;
try {
inStream = new BufferedInputStream(new FileInputStream(
localFilePath));
success = this.ftpClient.storeFile(remoteFileName, inStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
/
* 上传文件
*
* @param localFilePath
* 本地文件
* @param remoteFileName
* FTP 服务器文件名称
* @return
*/
public boolean uploadFile(File localFile, String remoteFileName) {
BufferedInputStream inStream = null;
boolean success = false;
try {
inStream = new BufferedInputStream(new FileInputStream(localFile));
success = this.ftpClient.storeFile(remoteFileName, inStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
/
* 变更工作目录
*
* @param remoteDir--目录路径
*/
public void changeDir(String remoteDir) {
try {
this.ftpClient.changeWorkingDirectory(remoteDir);
LogUtil.info("变更工作目录为:" + remoteDir);
} catch (IOException e) {
e.printStackTrace();
LogUtil.exception("变更工作目录为:" + remoteDir + "时出错!");
LogUtil.exception(e.getMessage());
}
}
/
* 变更工作目录
*
* @param remoteDir--目录路径
*/
public void changeDir(String[] remoteDirs) {
String dir = "";
try {
for (int i = 0; i < remoteDirs.length; i++) {
this.ftpClient.changeWorkingDirectory(remoteDirs[i]);
dir = dir + remoteDirs[i] + "/";
}
LogUtil.info("变更工作目录为:" + dir);
} catch (IOException e) {
e.printStackTrace();
LogUtil.exception("变更工作目录为:" + dir + "时出错!");
LogUtil.exception(e.getMessage());
}
}
/
* 返回上级目录
*
*/
public void toParentDir(String[] remoteDirs) {
try {
for (int i = 0; i < remoteDirs.length; i++) {
this.ftpClient.changeToParentDirectory();
}
LogUtil.info("返回上级目录");
} catch (IOException e) {
e.printStackTrace();
LogUtil.exception("返回上级目录时出错!");
LogUtil.exception(e.getMessage());
}
}
/
* 返回上级目录
*
*/
public void toParentDir() {
try {
this.ftpClient.changeToParentDirectory();
LogUtil.info("返回上级目录");
} catch (IOException e) {
e.printStackTrace();
LogUtil.exception("返回上级目录时出错!");
LogUtil.exception(e.getMessage());
}
}
/
* 获得FTP 服务器下所有的文件名列表
*
* @param regex
* @return
*/
public String[] getListFiels() {
String files[] = null;
try {
files = this.ftpClient.listNames();
} catch (IOException e) {
e.printStackTrace();
}
return files;
}
public FTPClient getFtpClient() {
return ftpClient;
}
public FTPModel getFtpModel() {
return ftpModel;
}
public void setFtpModel(FTPModel ftpModel) {
this.ftpModel = ftpModel;
}
}
2。采用FTP4J:
Java代码
/
* ftp4j FTP操作共公类
*
* @author 张明学
*
*/
public class FTP4JCommon {
FTPClient ftpClient = null;
FTPModel ftpModel = null;
public FTP4JCommon() {
}
public FTP4JCommon(FTPModel ftpModel) {
this.ftpModel = ftpModel;
}
/
* 连接并登录FTP服务器
*
*/
public boolean ftpLogin() {
ftpClient = new FTPClient();
try {
// 建立连接
ftpClient.connect(ftpModel.getUrl());
ftpClient.setType(FTPClient.TYPE_AUTO);
ftpClient.setCharset("GBK");
} catch (Exception e) {
e.printStackTrace();
LogUtil.infoOutPut("与FTP服务器建立连接失败!");
LogUtil.exception(e.getMessage());
}
try {
ftpClient.login(ftpModel.getUsername(), ftpModel.getPassword());
} catch (Exception e) {
e.printStackTrace();
LogUtil.infoOutPut("登录FTP服务器失败!");
LogUtil.exception(e.getMessage());
}
return true;
}
/
* 退出并关闭FTP连接
*
*/
public void close() {
if (null != ftpClient) {
try {
ftpClient.disconnect(true);
} catch (Exception e) {
e.printStackTrace();
LogUtil.infoOutPut("安全退出FTP服务时异常!");
LogUtil.exception(e.getMessage());
}
}
}
/
* 下载文件
*
* @param localFilePath
* 本地文件名及路径
* @param remoteFileName
* 远程文件名称
* @return
* @throws Exception
*/
public void downloadFile(String localFilePath, String remoteFileName)
throws Exception {
File localFile = new File(localFilePath);
try {
ftpClient.download(remoteFileName, localFile);
} catch (Exception e) {
e.printStackTrace();
LogUtil.infoOutPut("下载" + remoteFileName + "时出现异常!");
LogUtil.exception(e.getMessage());
throw e;
}
}
/
* 下载文件
*
* @param localFilePath
* 本地文件名及路径
* @param remoteFileName
* 远程文件名称
* @return
* @throws Exception
*/
public void downloadFile(File localFile, String remoteFileName)
throws Exception {
try {
ftpClient.download(remoteFileName, localFile);
} catch (Exception e) {
e.printStackTrace();
LogUtil.infoOutPut("下载" + remoteFileName + "时出现异常!");
LogUtil.exception(e.getMessage());
throw e;
}
}
/
* 获得FTP 服务器下所有的文件名列表
*
* @param regex
* @return
*/
public String[] getListFiels() {
String fileNames[] = null;
try {
fileNames = this.ftpClient.listNames();
} catch (Exception e) {
e.printStackTrace();
LogUtil.infoOutPut("获取文件名列表时出现异常!");
LogUtil.exception(e.getMessage());
}
return fileNames;
}
public FTPModel getFtpModel() {
return ftpModel;
}
public void setFtpModel(FTPModel ftpModel) {
this.ftpModel = ftpModel;
}
public FTPClient getFtpClient() {
return ftpClient;
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/61901.html