最近因工作需要,数据库中的数据需要从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:
3。采用:jftp下载
上面都用到了一个ftpModel如下(get,set方法省了):
- /**
- * FTP实体对象
- *
- * @author 张明学
- *
- */
- public class FTPModel {
-
- private String ftpId;
-
- private String username;
-
- private String password;
-
- private String url;
-
- private int port;
-
- private String remoteDir;
-
- public FTPModel() {
-
- }
-
- public FTPModel(String username, String password, String url, int port,
- String remoteDir) {
- this.username = username;
- this.password = password;
- this.url = url;
- this.port = port;
- this.remoteDir = remoteDir;
- }
- }
上述仅仅列出了一点常用的操作,更多的操作需要参看它的们API文件。
还有,我采用的Serv-U作为FTP服务器,按装之后新建用户不法登录。后来找到了原因是:要把IIS服务中的FTP服务关掉。
|