使用apache的commons-net-3.1.jar实现FTP上传

commons-net-3.1.jar下载地址

在有些项目中我们需要将文件保存在FTP中,

但是在网上介绍FTP上传的知识总是感觉很混乱,

现在将网上的一些代码经过整理与测试,拿出来与大家分享。

有些地方写的可能不是很好,还望大家见谅。

我觉得在向FTP上传文件的时候上传文件并不是最难的,而是如何在FTP上创建文件夹。

package com.ftp.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class FtpDemo {
	/**
	 * 连接FTP
	 * 
	 * @param ip
	 * @param port
	 * @param userName
	 * @param password
	 * @param file
	 * @param remotePathName
	 * @param remoteName
	 */
	private static void upload(String ip, int port, String userName,
			String password, File file, String remotePathName, String remoteName) {
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.connect(ip, port);
			ftpClient.login(userName, password);
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
			ftpClient.enterLocalPassiveMode();

			// 上传文件
			upload(ftpClient, file, remotePathName, remoteName);

		} catch (Exception e) {
			System.out.println("fail to upload files : " + e.getMessage());
		} finally {
			if (ftpClient != null && ftpClient.isConnected()) {
				try {
					ftpClient.disconnect();
				} catch (IOException e) {
					System.out.println("ftp fails to disconnect : "
							+ e.getMessage());
				}
			}
		}
	}

	/**
	 * 上传文件
	 * 
	 * @param ftpClient
	 * @param file
	 * @param remotePathName
	 * @param remoteName
	 */
	private static void upload(FTPClient ftpClient, File file,
			String remotePathName, String remoteName) throws Exception {
		try {
			// 1. 切目录
			changeDirectory(ftpClient, remotePathName);
			// 2. 传文件
			uploadFile(ftpClient, file, remotePathName, remoteName);
			// 3. 切回根目录
			backToRootDirectory(ftpClient);
		} catch (IOException e) {
		}
	}

	/**
	 * 切换目录
	 * 
	 * @param ftpClient
	 * @param path
	 * @throws IOException
	 */
	private static void changeDirectory(FTPClient ftpClient, String path)
			throws IOException {
		int nextSeperator = path.indexOf("/", 1);
		String currentPath = null;
		if (nextSeperator < 0) {
			nextSeperator = path.length();
			currentPath = path.substring(1, nextSeperator);
			changeDirectory0(ftpClient, currentPath);
			return;
		} else {
			currentPath = path.substring(1, nextSeperator);
			changeDirectory0(ftpClient, currentPath);
			changeDirectory(ftpClient, path.substring(nextSeperator));
		}
	}

	/**
	 * 如果切换的目录不存在 就创建一个新的目录
	 * 
	 * @param ftpClient
	 * @param path
	 * @throws IOException
	 */
	private static void changeDirectory0(FTPClient ftpClient, String path)
			throws IOException {
		if (!ftpClient.changeWorkingDirectory(path)) {
			ftpClient.makeDirectory(path);
		}
		ftpClient.changeWorkingDirectory(path);
	}

	/**
	 * 回到根目录根
	 * 
	 * @param ftpClient
	 * @throws IOException
	 */
	private static void backToRootDirectory(FTPClient ftpClient)
			throws IOException {
		ftpClient.changeWorkingDirectory("/");
	}

	/**
	 * 真正的上传文件方法
	 * 
	 * @param ftpClient
	 * @param file
	 * @param remotePathName
	 * @param remoteName
	 */
	private static void uploadFile(FTPClient ftpClient, File file,
			String remotePathName, String remoteName) throws Exception {
		String localPathName = file.getAbsolutePath();
		FTPFile[] files = ftpClient.listFiles(remoteName);
		if (files.length == 1) {
			if (!ftpClient.deleteFile(remoteName)) {
				throw new Exception("fail to delete remote file ["
						+ remotePathName + "] before uploading");
			}
		}
		File f = new File(localPathName);
		InputStream is = new FileInputStream(f);
		if (!ftpClient.storeFile(remoteName, is)) {
			is.close();
		}
		is.close();
	}
}



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值