ftp上传文件(jftp)

来源:互联网 发布:粉钥匙软件 编辑:程序博客网 时间:2024/06/03 20:56

之前有过ftp下载文件的一个例子,用的是FTPClient

这里再介绍一个用jftp的  这里先做上传文件的功能,下载说是跟这差不多,本人愚钝暂时还没弄明白稍后续上

引用的jar包是jftp.jar 这个直接实现ConnectionListener的一系列方法就可以了用起来更方便一些

 

import net.sf.jftp.net.BasicConnection;
import net.sf.jftp.net.ConnectionHandler;
import net.sf.jftp.net.ConnectionListener;
import net.sf.jftp.net.FtpConnection;

import org.apache.commons.lang.StringUtils;

import thread.UploadThread;

/**
 * ftp上传文件
 *
 * @author JohnYang
 *
 */
public class FtpUpload implements ConnectionListener {

 private boolean isThere = false;

 private ConnectionHandler handler = new ConnectionHandler();

 private String host;
 private int port = 21;
 private String user;
 private String passwd;

 public FtpUpload(String host, String user, String passwd) {
  this.host = host;
  this.user = user;
  this.passwd = passwd;
 }

 public FtpUpload(String host, int port, String user, String passwd) {
  this.host = host;
  this.port = port;
  this.user = user;
  this.passwd = passwd;
 }

 public int upload(String dir, String file) {
  FtpConnection con = new FtpConnection(host, port, "/");

  con.addConnectionListener(this);

  con.setConnectionHandler(handler);

  con.login(user, passwd);

  while (!isThere) {
   try {
    Thread.sleep(10);
   } catch (Exception ex) {
    ex.printStackTrace();
   }
  }
  // make dirs
  String path = "";
  String[] paths = StringUtils.split(dir, "/");
  for (int i = 0; i < paths.length; i++) {
   path += "/" + paths[i];
   if (!con.chdir(path)) {
    con.mkdir(path);
   }
  }
  con.chdir(dir);
  return con.upload(file);
 }

 public void actionFinished(BasicConnection con) {

 }

 public void connectionFailed(BasicConnection con, String why) {
  //System.out.println("connection failed!");

 }

 public void connectionInitialized(BasicConnection con) {
  isThere = true;
 }

 public void updateProgress(String file, String type, long bytes) {

 }

 public void updateRemoteDirectory(BasicConnection con) {
 // System.out.println("new path is: " + con.getPWD());

 }

 public boolean isThere() {
  return isThere;
 }

 public void setThere(boolean isThere) {
  this.isThere = isThere;
 }

 public String getHost() {
  return host;
 }

 public void setHost(String host) {
  this.host = host;
 }

 public int getPort() {
  return port;
 }

 public void setPort(int port) {
  this.port = port;
 }

 public String getUser() {
  return user;
 }

 public void setUser(String user) {
  this.user = user;
 }

 public String getPasswd() {
  return passwd;
 }

 public void setPasswd(String passwd) {
  this.passwd = passwd;
 }

 public ConnectionHandler getHandler() {
  return handler;
 }

 public void setHandler(ConnectionHandler handler) {
  this.handler = handler;
 }

 

/**
  * 启用多线程上传文件
  * @param argv
  */ 

public static void main(String argv[]) {
  UploadThread uploadThread = new UploadThread();
  for (int i = 0; i < 10; i++) {
   uploadThread.start();
  }
 }
}

//线程类 当然这个可以根据情况看是否需要

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import ftp.FtpUpload;

public class UploadThread extends Thread {
 @SuppressWarnings("unchecked")
 public void run() {
  List<String> list = new ArrayList<String>();
  if (getFileList().length > 0) {
   for (int i = 0; i < getFileList().length; i++) {
    list.add(getFileList()[i]);
   }
  }
  FtpUpload g = new FtpUpload("192.168.1.147", 21, "userName",
    "password");
  for (int i = 0; i < getFileList().length; i++) {
   DownLoadThread th = new DownLoadThread();
   th.start();
   g.upload("/", "D:/ftp2/" + getFileList()[i]);
  }

 }

 /**
  * 获得文件夹下所有文件列表
  *
  * @return
  */
 public static String[] getFileList() {
  File file = new File("D:/ftp2");
  String[] fileList = null;
  if (file.isDirectory()) {
   System.out.println(file.getName());
   fileList = file.list();
  } else {
   System.out.println("这不是个文件目录谢谢!!");
  }
  return fileList;
 }

}