天天看点

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;

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

 }

}