您的位置:首页 > 运维架构 > Apache

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

2012-09-19 14:12 423 查看
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();
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐