使用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);
  try {
   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();
  } catch (Exception e) {
   e.printStackTrace();
  }

  return result;
 }
 
 public static void logout(FTPClient ftp) {
  try {
   if (ftp != null) {
    ftp.logout();
    ftp.disconnect();
   }
   ftp = null;
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}


测试代码;


import java.io.FileNotFoundException;
import java.io.IOException;

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

 

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
   e.printStackTrace();
  }
    
  try {
   ftp.logout();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  if (ftp.isConnected()) {
   try {
    ftp.disconnect();
   } catch (IOException ioe) {
    ioe.printStackTrace();
   }
  }
  
  

 }

}


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


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值