java实现FTP传输文件( 2008-05-23 15:45:09| 分类: java 技术)

本文介绍如何使用Apache Commons Net库实现FTP服务器的连接、文件上传、下载、删除及目录创建与删除等功能,适用于处理包含中文字符集的文件名与路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPFile;

import org.apache.commons.net.ftp.FTPReply;

/**

 * @author xuyang 导入commons-net-1.4.1.jar、jakarta-oro-2.0.8.jar这两个包

 * 文件、文件夹的名称有中文,先转换字符集

 * 如:new String(fileName.getBytes("gb2312"),"iso8859-1");

 */

public class FtpUtil {

 private FTPClient ftp;

 /**

  * 连接FTP服务器

  *

  * @param serverIP

  * @param user

  * @param password

  * @return boolean

  */

 public boolean connectFtpServer(String serverIP, String user,

   String password) {

  ftp = new FTPClient();

  int reply;

  try {

   ftp.connect(serverIP);

   reply = ftp.getReplyCode();

   if (!FTPReply.isPositiveCompletion(reply)) {

    ftp.disconnect();

    return false;

   }

   if (!ftp.login(user, password)) {

    disconnect();

    return false;

   }

  } catch (IOException e) {

   disconnect();

   return false;

  }

  return true;

 }

 /**

  * 创建文件夹

  *

  * @param path

  * @return boolean

  */

 public boolean createDirectory(String path) {

  try {

   String[] dirArray = path.split("/");

   for (int i = 0; i < dirArray.length; i++) {

    if (dirArray[i] != null)

     if (!ftp.changeWorkingDirectory(dirArray[i])) {

      ftp.makeDirectory(dirArray[i]);

      ftp.changeWorkingDirectory(dirArray[i]);

     }

   }

  } catch (IOException e) {

   disconnect();

   return false;

  }

  return true;

 }

 /**

  * 下载文件

  *

  * @param local

  * @param remote

  * @return boolean

  */

 public boolean downloadFile(String local, String remote) {

  OutputStream output = null;

  try {

   ftp.setFileType(FTP.BINARY_FILE_TYPE);

   ftp.enterLocalPassiveMode();

   String[] dirArray = remote.split("/");

   if (dirArray.length > 1) {

    for (int i = 0; i < dirArray.length - 1; i++) {

     if (dirArray[i] != null) {

      ftp.changeWorkingDirectory(dirArray[i]);

     }

    }

   }

   File file = new File(local);

   output = new FileOutputStream(file);

   ftp.retrieveFile(dirArray[dirArray.length - 1], output);

   output.close();

  } catch (IOException e) {

   disconnect();

   return false;

  }

  return true;

 }

 /**

  * 上传文件

  *

  * @param local

  * @param remote

  * @return boolean

  */

 public boolean uploadFile(String local, String remote) {

  InputStream input = null;

  try {

   ftp.setFileType(FTP.BINARY_FILE_TYPE);

   ftp.enterLocalPassiveMode();

   String[] dirArray = remote.split("/");

   if (dirArray.length > 1) {

    for (int i = 0; i < dirArray.length - 1; i++) {

     if (dirArray[i] != null) {

      ftp.changeWorkingDirectory(dirArray[i]);

     }

    }

   }

   input = new FileInputStream(local);

   ftp.storeFile(dirArray[dirArray.length - 1], input);

   input.close();

  } catch (IOException e) {

   disconnect();

   return false;

  }

  return true;

 }

 /**

  * 删除文件

  *

  * @param filePath

  * @return boolean

  */

 public boolean deleteFile(String filePath) {

  try {

   String[] dirArray = filePath.split("/");

   if (dirArray.length > 1) {

    for (int i = 0; i < dirArray.length - 1; i++) {

     if (dirArray[i] != null) {

      ftp.changeWorkingDirectory(dirArray[i]);

     }

    }

   }

   ftp.deleteFile(dirArray[dirArray.length - 1]);

  } catch (IOException e) {

   disconnect();

   return false;

  }

  return true;

 }

 /**

  * 删除文件夹

  *

  * @param path

  * @return boolean

  */

 public boolean removeDirectory(String path) {

  try {

   String[] dirArray = path.split("/");

   for (int i = 0; i < dirArray.length; i++) {

    if (dirArray[i] != null) {

     ftp.changeWorkingDirectory(dirArray[i]);

    }

   }

   FTPFile[] files = ftp.listFiles();

   for (int j = 0; j < files.length; j++) {

    if (files[j].isDirectory()) { // 递归删除子文件夹

     removeDirectory(files[j].getName());

    }

    ftp.deleteFile(files[j].getName());

   }

   ftp.changeToParentDirectory();

   ftp.removeDirectory(dirArray[dirArray.length - 1]);

  } catch (IOException e) {

   disconnect();

   return false;

  }

  return true;

 }

 /**

  * 重命名文件、文件夹

  *

  * @param from

  * @param to

  * @return boolean

  */

 public boolean rename(String from, String to) {

  try {

   String[] dirArray = from.split("/");

   if (dirArray.length > 1) {

    for (int i = 0; i < dirArray.length - 1; i++) {

     if (dirArray[i] != null) {

      ftp.changeWorkingDirectory(dirArray[i]);

     }

    }

   }

   ftp.rename(dirArray[dirArray.length - 1], to);

  } catch (IOException e) {

   disconnect();

   return false;

  }

  return true;

 }

 /**

  * 断开连接

  */

 public void disconnect() {

  if (ftp.isConnected()) {

   try {

    ftp.logout();

    ftp.disconnect();

   } catch (IOException e) {

    e.printStackTrace();

   }

  }

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值