天天看點

使用apache commons-net包實作檔案ftp上傳

使用apache commons-net包實作檔案ftp上傳

import java.io.file;

import java.io.fileinputstream;

import java.io.filenotfoundexception;

import java.io.ioexception;

import java.net.sockettimeoutexception;

import org.apache.commons.net.ftp.ftp;

import org.apache.commons.net.ftp.ftpclient;

import org.apache.commons.net.ftp.ftpreply;

public class ftptool {

 private static ftpclient ftp;

 public static ftpclient ftp_conn(string server, string user, string password) {

  ftp = new ftpclient();

  // ftp.setdefaulttimeout(5000);

  try {

   int reply;

   ftp.connect(server);

   // ftp.connect(server,21,inetaddress.getlocalhost(),21);

   system.out.println("connected to " + server + ".");

   system.out.println(ftp.getreplystring());

   reply = ftp.getreplycode();

   if (!ftpreply.ispositivecompletion(reply)) {

    ftp.disconnect();

    system.out.println("ftp server refused connection.");

    return null;

   } else {

    ftp.login(user, password);

    system.out.println("login success.");

    ftp.pasv();

    ftp.enterlocalpassivemode();

   }

  } catch (sockettimeoutexception ste) {

   ste.printstacktrace();

  } catch (exception e) {

   e.printstacktrace();

  }

  return ftp;

 }

 /**

  * @param ftp

  * @param remotefile

  * @param localfile

  * @return

  * @throws filenotfoundexception

  */

 public static boolean uploadtoftp(ftpclient ftp, string remotefile,

   string localfile) throws filenotfoundexception {

  boolean result = false;

  if (ftp == null) {

   return result;

  string dir = "/";

  remotefile = remotefile.replaceall("\\\\", "/");

  if (remotefile.indexof("/") != -1) {

   dir = (string) remotefile.subsequence(0, remotefile.lastindexof("/"));

  fileinputstream fis = new fileinputstream(new file(localfile));

  system.out.println("upload " + localfile + " to " + remotefile);

   ftp.makedirectory(dir);

   ftp.changeworkingdirectory(dir);

   ftp.setfiletype(ftp.binary_file_type); // 以binary格式傳送檔案

   if (ftp.storefile(remotefile, fis)) {

    result = true;

   // system.out.println(ftp.getreplycode());

   fis.close();

  return result;

 public static void logout(ftpclient ftp) {

   if (ftp != null) {

    ftp.logout();

   ftp = null;

  } catch (ioexception e) {

}

測試代碼;

public class ftptest {

  * @param args

 public static void main(string[] args) {

  string ftp_server = "192.168.0.1";

  string ftp_user = "username";

  string ftp_password ="password";

  ftpclient ftp = ftptool.ftp_conn(ftp_server, ftp_user, ftp_password);

  string localfilename="d:\\html\\test.html";

  string remotefilename="/mytest/002/test.html";

     system.out.println("upload ...");

     system.out.println(localfilename + " to " + remotefilename);

     try {

      ftptool.uploadtoftp(ftp, remotefilename, localfilename);

  } catch (filenotfoundexception e) {

   // todo auto-generated catch block

   ftp.logout();

  if (ftp.isconnected()) {

   try {

   } catch (ioexception ioe) {

    ioe.printstacktrace();

相關jar包: apache commons-net-1.4.1.jar