java使用commons-net-2.2访问ftp

1 篇文章 0 订阅
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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

/**
 * ftp操作工具类
 * @author dell
 *
 */
public class FtpClientUtil {

	/**
	 * 打开ftp服务器连接
	 * 
	 * @param ftpClient
	 * @param server
	 *            ftp的url
	 * @param port
	 *            端口
	 * @param userName
	 *            登陆用户名
	 * @param password
	 *            登陆密码
	 * @return
	 */
	public static boolean open(FTPClient ftpClient, String server, int port,
			String userName, String password) {
		if (ftpClient == null) {
			ftpClient = new FTPClient();
		}

		// 已经打开连接则直接返回
		if (ftpClient.isConnected())
			return true;

		try {
		//	ftpClient.setControlEncoding("GBK");
			
			// 连接服务器
			ftpClient.connect(server, port);

			ftpClient.login(userName, password);

			if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
				ftpClient.disconnect();
				return false;
			}
			
			ftpClient.setControlEncoding("GBK");

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			ftpClient = null;
			return false;
		}

	}

	/**
	 * 打开ftp上某个目录
	 * 
	 * @param ftpClient
	 * @param dir
	 * @return
	 */
	public static boolean cd(FTPClient ftpClient, String dir) {
		boolean f = false;
		try {
			ftpClient.changeWorkingDirectory(dir);
		} catch (IOException e) {

			return f;
		}
		return true;
	}

	/**
	 * 取得指定路径下的满足要求的文件名列表
	 * 
	 * @param ftpClient
	 * @param server
	 * @param port
	 * @param userName
	 * @param password
	 * @param dir
	 *            ftp路径
	 * @param begin
	 *            文件名过滤,以什么字符串开始,不需要此过滤传入null
	 * @param end
	 *            文件名过滤,以什么字符串结束,不需要此过滤传入null
	 * @return 出错返回null,正常为文件名list
	 */
	public static List<String> files(FTPClient ftpClient, String dir,
			String begin, String end) {

		try {
			// 取得文件名流
			cd(ftpClient, dir);

			FTPFile[] names = ftpClient.listFiles();

			List<String> files = new ArrayList<String>();

			// 读取文件名
			for (FTPFile name : names) {

				// 文件名过滤
				if (begin != null && !name.getName().startsWith(begin)) {
					continue;
				}
				if (end != null && !name.getName().endsWith(end)) {
					continue;
				}

				files.add(name.getName());
			}

			return files;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}

	/**
	 * 上传文件到FTP服务器
	 * 
	 * @param is
	 *            本地文件流
	 * @param ftpFileName
	 *            上传后的文件名
	 * @param ftpDirectory
	 *            FTP目录如:/path1/pathb2/,如果目录不存在回自动创建目录
	 * @throws Exception
	 */
	public static boolean upload(FTPClient ftpClient, InputStream is,
			String ftpFileName, String ftpDirectory) throws Exception {

		try {
			ftpClient.makeDirectory(ftpDirectory);

			cd(ftpClient, ftpDirectory);
			ftpClient.storeFile(ftpFileName, is);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 从FTP服务器上下载文件并返回文件流
	 * 
	 * @param ftpDirAndFileName
	 * @param ftpClient
	 * @return
	 * @throws Exception
	 */
	public static InputStream download2File(FTPClient ftpClient,
			String ftpDirAndFileName) throws Exception {

		try {
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			return ftpClient.retrieveFileStream(charEncode(ftpDirAndFileName));

		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * 从FTP服务器上下载文件并返回文件流
	 * 
	 * @param ftpDirAndFileName
	 * @param localDirAndFileName
	 * @param ftpClient
	 * @return
	 * @throws Exception
	 */
	public static OutputStream download2File(FTPClient ftpClient,
			String ftpDirAndFileName, File f)
			throws Exception {

		try {
			OutputStream os = new FileOutputStream(f);
			ftpClient.setBufferSize(1024);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.retrieveFile(charEncode(ftpDirAndFileName), os);
			return os;

		} catch (Exception e) {
			return null;
		}

	}

	/**
	 * 删除FTP上的文件
	 * 
	 * @param ftpDirAndFileName
	 */
	public static boolean deleteFile(FTPClient ftpClient,
			String ftpDirAndFileName) throws Exception {

		try {
			ftpClient.deleteFile(charEncode(ftpDirAndFileName));
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 关闭链接
	 */
	public static void close(FTPClient ftpClient) {

		try {
			if (ftpClient != null && ftpClient.isConnected())
				ftpClient.logout();
			ftpClient.disconnect();
		} catch (Exception e) {

		}

	}

	/**
	 * 转码
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String charEncode(String str) throws Exception {
		return new String((str).getBytes("GBK"), "iso-8859-1");
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
commons-io-2.2.jar是一个用于Java开发的常用依赖包。它提供了许多实用的工具类和方法,用于简化文件和流的输入输出操作。 commons-io-2.2.jar包含了多个类,其中一些主要的类和功能包括: 1. FileUtils类:提供了一系列静态方法,用于操作文件,如复制、移动、删除、比较文件等。 2. IOUtils类:提供了一系列静态方法,用于操作输入输出流,如读取、写入、关闭流等。 3. FilenameUtils类:提供了一系列静态方法,用于操作文件名,如获取名字、扩展名、路径等。 4. IOCase类:提供了一系列枚举常量,用于指定文件名比较的大小写敏感性。 使用commons-io-2.2.jar依赖包,可以在项目中轻松地进行文件和流的处理。例如,可以使用FileUtils类的静态方法复制文件、移动文件或删除文件。使用IOUtils类的静态方法可以方便地读取文件内容、写入文件内容,同时还可以保证在操作完毕后,流会被正确地关闭。 此外,commons-io-2.2.jar还提供了许多其他有用的功能,例如操作文件的符号链接、文件滚动、文件比较等。它具有良好的性能和稳定性,并且广泛用于各种Java项目中。 总结而言,commons-io-2.2.jar是一个重要的Java依赖包,它提供了简化文件和流的输入输出操作的工具类和方法。通过使用此依赖包,可以方便地进行文件和流的操作,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wutian4567268

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值