apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
来源:互联网 发布:淘宝免费模块代码 编辑:程序博客网 时间:2024/04/30 14:43
原文地址:http://blog.csdn.net/duanduanlong/article/details/7569324
用到了apache 的 commons-net-3.0.1.jar 和 log4j-1.2.15.jar 这连个jar包
JAVA 代码如下:
- package com.bjut.edu.cn.ftp;
-
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.TimeZone;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPClientConfig;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
-
- import org.apache.log4j.Logger;
-
- public class Ftp {
- private FTPClient ftpClient;
- private String strIp;
- private int intPort;
- private String user;
- private String password;
-
- private static Logger logger = Logger.getLogger(Ftp.class.getName());
-
-
-
-
- public Ftp(String strIp, int intPort, String user, String Password) {
- this.strIp = strIp;
- this.intPort = intPort;
- this.user = user;
- this.password = Password;
- this.ftpClient = new FTPClient();
- }
-
-
-
- public boolean ftpLogin() {
- boolean isLogin = false;
- FTPClientConfig ftpClientConfig = new FTPClientConfig();
- ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
- this.ftpClient.setControlEncoding("GBK");
- this.ftpClient.configure(ftpClientConfig);
- try {
- if (this.intPort > 0) {
- this.ftpClient.connect(this.strIp, this.intPort);
- } else {
- this.ftpClient.connect(this.strIp);
- }
-
- int reply = this.ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- this.ftpClient.disconnect();
- logger.error("登录FTP服务失败!");
- return isLogin;
- }
- this.ftpClient.login(this.user, this.password);
-
- this.ftpClient.enterLocalPassiveMode();
- this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- logger.info("恭喜" + this.user + "成功登陆FTP服务器");
- isLogin = true;
- } catch (Exception e) {
- e.printStackTrace();
- logger.error(this.user + "登录FTP服务失败!" + e.getMessage());
- }
- this.ftpClient.setBufferSize(1024 * 2);
- this.ftpClient.setDataTimeout(30 * 1000);
- return isLogin;
- }
-
-
-
-
- public void ftpLogOut() {
- if (null != this.ftpClient && this.ftpClient.isConnected()) {
- try {
- boolean reuslt = this.ftpClient.logout();
- if (reuslt) {
- logger.info("成功退出服务器");
- }
- } catch (IOException e) {
- e.printStackTrace();
- logger.warn("退出FTP服务器异常!" + e.getMessage());
- } finally {
- try {
- this.ftpClient.disconnect();
- } catch (IOException e) {
- e.printStackTrace();
- logger.warn("关闭FTP服务器的连接异常!");
- }
- }
- }
- }
-
-
-
-
-
-
- public boolean uploadFile(File localFile, String romotUpLoadePath) {
- BufferedInputStream inStream = null;
- boolean success = false;
- try {
- this.ftpClient.changeWorkingDirectory(romotUpLoadePath);
- inStream = new BufferedInputStream(new FileInputStream(localFile));
- logger.info(localFile.getName() + "开始上传.....");
- success = this.ftpClient.storeFile(localFile.getName(), inStream);
- if (success == true) {
- logger.info(localFile.getName() + "上传成功");
- return success;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- logger.error(localFile + "未找到");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (inStream != null) {
- try {
- inStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return success;
- }
-
-
-
-
-
-
-
-
- public boolean downloadFile(String remoteFileName, String localDires,
- String remoteDownLoadPath) {
- String strFilePath = localDires + remoteFileName;
- BufferedOutputStream outStream = null;
- boolean success = false;
- try {
- this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
- outStream = new BufferedOutputStream(new FileOutputStream(
- strFilePath));
- logger.info(remoteFileName + "开始下载....");
- success = this.ftpClient.retrieveFile(remoteFileName, outStream);
- if (success == true) {
- logger.info(remoteFileName + "成功下载到" + strFilePath);
- return success;
- }
- } catch (Exception e) {
- e.printStackTrace();
- logger.error(remoteFileName + "下载失败");
- } finally {
- if (null != outStream) {
- try {
- outStream.flush();
- outStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- if (success == false) {
- logger.error(remoteFileName + "下载失败!!!");
- }
- return success;
- }
-
-
-
-
-
-
-
-
- public boolean uploadDirectory(String localDirectory,
- String remoteDirectoryPath) {
- File src = new File(localDirectory);
- try {
- remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";
- this.ftpClient.makeDirectory(remoteDirectoryPath);
-
- } catch (IOException e) {
- e.printStackTrace();
- logger.info(remoteDirectoryPath + "目录创建失败");
- }
- File[] allFile = src.listFiles();
- for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
- if (!allFile[currentFile].isDirectory()) {
- String srcName = allFile[currentFile].getPath().toString();
- uploadFile(new File(srcName), remoteDirectoryPath);
- }
- }
- for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
- if (allFile[currentFile].isDirectory()) {
-
- uploadDirectory(allFile[currentFile].getPath().toString(),
- remoteDirectoryPath);
- }
- }
- return true;
- }
-
-
-
-
-
-
- public boolean downLoadDirectory(String localDirectoryPath,String remoteDirectory) {
- try {
- String fileName = new File(remoteDirectory).getName();
- localDirectoryPath = localDirectoryPath + fileName + "//";
- new File(localDirectoryPath).mkdirs();
- FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory);
- for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
- if (!allFile[currentFile].isDirectory()) {
- downloadFile(allFile[currentFile].getName(),localDirectoryPath, remoteDirectory);
- }
- }
- for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
- if (allFile[currentFile].isDirectory()) {
- String strremoteDirectoryPath = remoteDirectory + "/"+ allFile[currentFile].getName();
- downLoadDirectory(localDirectoryPath,strremoteDirectoryPath);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- logger.info("下载文件夹失败");
- return false;
- }
- return true;
- }
-
- public FTPClient getFtpClient() {
- return ftpClient;
- }
- public void setFtpClient(FTPClient ftpClient) {
- this.ftpClient = ftpClient;
- }
-
- public static void main(String[] args) throws IOException {
- Ftp ftp=new Ftp("10.3.15.1",21,"ghips","ghipsteam");
- ftp.ftpLogin();
-
- ftp.uploadDirectory("d://DataProtemp", "/home/data/");
-
- ftp.downLoadDirectory("d://tmp//", "/home/data/DataProtemp");
- ftp.ftpLogOut();
- }
- }
0 0