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

本文介绍了一个Java程序,通过FtpClient类实现在FTP服务器上创建目录并上传文件。Ftp类包含了构造方法、上传文件的方法upload以及辅助函数如createDir和isDirExist。核心部分展示了如何使用FTP连接、文件操作和错误处理。
摘要由CSDN通过智能技术生成

package comm;

import java.io.File;

import java.io.FileInputStream;

import java.io.OutputStream;

import java.util.StringTokenizer;

import sun.net.ftp.FtpClient;

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;

}

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();

}

private Boolean isDirExist(String dir, FtpClient ftpClient) {

try {

ftpClient.cd(dir);

} catch (Exception e) {

return false;

}

return true;

}

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;

}

}

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() +

"]有误或不存在!");

}

}

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");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值