java 实现FTP上传文件夹,包括整个目录

package comm;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
import sun.net.ftp.FtpClient;

/**
* ftp上传
* @author colin
* @version 1.0
*
*/
public class Ftp {

private String ip = "";

private String username = "";

private String password = "";

private int port = 21;

private String localFileFullName = "";//需要上传的目录

FtpClient ftpClient = null;

OutputStream os = null;

FileInputStream is = null;

public Ftp(String serverIP, int port , String username, String password) {

this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;

}

public Ftp(String serverIP,String username, String password) {

this.ip = serverIP;
this.username = username;
this.password = password;

}

/**
* 创建文件夹
* @param dir
* @param ftpClient
* @throws Exception
*/
private void createDir(String dir, FtpClient ftpClient) throws Exception {
ftpClient.ascii();
StringTokenizer s = new StringTokenizer(dir, "/"); //sign
s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpClient.sendServer("MKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
}
ftpClient.readServerResponse();
}
ftpClient.binary();

}

/**
* 检查文件夹是否存在
* @param dir
* @param ftpClient
* @return
*/
private Boolean isDirExist(String dir, FtpClient ftpClient) {
try {
ftpClient.cd(dir);
} catch (Exception e) {

return false;
}
return true;
}

/**
* ftp上传
* @param localFileFullName 上传的源文件夹
* @return
*/
public Boolean upload(String localFileFullName) {
this.localFileFullName = localFileFullName;
try {
String savefilename = new String(localFileFullName.getBytes("ISO-8859-1"), "GBK");
//新建一个FTP客户端连接
ftpClient = new FtpClient();
ftpClient.openServer(this.ip,this.port);
ftpClient.login(this.username, this.password);
//打开本地待长传的文件
File file_in = new File(savefilename);
processFile(file_in,ftpClient);
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (ftpClient != null) {
ftpClient.closeServer();
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception e in Ftp upload(): " + e.toString());
return false;
}

}

/**
* 上传文件
* @param source
* @param ftpClient
* @throws Exception
*/
private void processFile(File source,FtpClient ftpClient) throws Exception{
if (source.exists()) {
if (source.isDirectory()) {
if (!isDirExist(source.getPath().substring(localFileFullName.length()).replace("\\", "/"), ftpClient)) {
createDir(source.getPath().substring(localFileFullName.length()).replace("\\", "/"), ftpClient);
}
File sourceFile[] = source.listFiles();
for (int i = 0; i < sourceFile.length; i++) {
if (sourceFile[i].exists()) {
if (sourceFile[i].isDirectory()) {
this.processFile(sourceFile[i],ftpClient);
} else {
ftpClient.cd(cheangPath(sourceFile[i].getPath()));
ftpClient.binary();
os = ftpClient.put(sourceFile[i].getName());
byte[] bytes = new byte[1024];
is = new FileInputStream(sourceFile[i]);
//开始复制
int c;
//暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
}
}
}
} else {
ftpClient.cd(cheangPath(source.getPath()));
ftpClient.binary();
os = ftpClient.put(source.getName());
byte[] bytes = new byte[1024];
is = new FileInputStream(source);
//开始复制
int c;
//暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
}

} else {
throw new Exception("此文件或文件夹[" + source.getName() + "]有误或不存在!");
}

}

/**
* 获取当前的FTP路径
* @param path
* @return
*/
private String cheangPath(String path){
path = path.substring(localFileFullName.length()).replace("\\", "/");
if("".equals(path)){
path = "/";
}else{
path = path.substring(0,path.lastIndexOf("/")+1);
}
return path;
}

public static void main(String args[]) throws Exception {
Ftp ftpup = new Ftp("127.0.0.1",21,"colin","colin");
ftpup.upload("E:/test/test");
}
}

可参考:http://wyz316688.iteye.com/blog/517974
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值