Jakarta Commons NET(FTPClient)的简单示例

本文介绍如何使用Java中的Jakarta Commons Net库(FTPClient)进行FTP文件上传和下载操作。示例代码展示了连接FTP服务器、登录验证、设置文件传输模式及实际文件传输的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ava中使用FTP传送文件或者取得文件,可以使用Jakarta Commons NET(FTPClient)的包来实现。

具体的示例如下:(例子是从网上拷贝的)

  1. package test.ftp;    
  2.     
  3. import java.io.FileInputStream;    
  4. import java.io.FileOutputStream;    
  5. import java.io.IOException;    
  6. import java.io.InputStream;    
  7. import java.io.OutputStream;    
  8.     
  9. import org.apache.commons.net.ftp.FTP;    
  10. import org.apache.commons.net.ftp.FTPClient;    
  11. import org.apache.commons.net.ftp.FTPReply;    
  12.     
  13.     
  14. public class FtpClientUtil {    
  15.     private static final int FTP_PORT = 21;    
  16.         
  17.     public static void main(String[] args) {    
  18.         try {    
  19.             //读入文件    
  20.             FileInputStream fis = new FileInputStream("c:\testftp.txt");    
  21.             //传送文件到FTP服务器  
  22.             FtpClientUtil.sendFile("localhost", FTP_PORT, "testuser""testpassword",   "remoteFilename", fis);    
  23.                 
  24.             //从FTP服务器取得文件    
  25.             FileOutputStream fos = new FileOutputStream("localfile");     
  26.             FtpClientUtil.retrieveFile("localhost", FTP_PORT, "testuser""testpassword",   "remoteFilename", fos);    
  27.                 
  28.         } catch (Exception e) {    
  29.             e.printStackTrace();    
  30.         }    
  31.     }    
  32.     
  33.     //上传文件    
  34.     public static void sendFile (String host,     
  35.             int port,    
  36.             String user,    
  37.             String password,    
  38.             String remoteFilename,    
  39.             InputStream is    
  40.             ) throws Exception {    
  41.         FTPClient ftpclient = new FTPClient();    
  42.             
  43.         try {    
  44.             //设置服务器名和端口  
  45.             ftpclient.connect(host, port);    
  46.             int reply = ftpclient.getReplyCode();    
  47.             if (!FTPReply.isPositiveCompletion(reply)) {    
  48.                 //连接错误的时候报错。   
  49.                 Exception ee = new Exception("Can't Connect to :" + host);    
  50.                 throw ee;    
  51.             }    
  52.                 
  53.             //登录    
  54.             if (ftpclient.login(user, password) == false) {    
  55.                 // invalid user/password    
  56.                 Exception ee = new Exception("Invalid user/password");    
  57.                 throw ee;    
  58.             }    
  59.     
  60.             //设置传送文件模式    
  61.             ftpclient.setFileType(FTP.BINARY_FILE_TYPE);    
  62.                 
  63.             //传送文件    
  64.             ftpclient.storeFile(remoteFilename, is);                                  
  65.         } catch (IOException e) {    
  66.             throw e;    
  67.         } finally {    
  68.             try {    
  69.                 ftpclient.disconnect(); //解除连接    
  70.             } catch (IOException e) {    
  71.             }    
  72.         }    
  73.             
  74.     }    
  75.         
  76.     //文件下载    
  77.     public static void retrieveFile(String host,     
  78.             int port,    
  79.             String user,    
  80.             String password,    
  81.             String remoteFilename,    
  82.             OutputStream os) throws Exception {    
  83.         FTPClient ftpclient = new FTPClient();    
  84.             
  85.         try {    
  86.             //设置服务器名和端口    
  87.             ftpclient.connect(host, port);    
  88.             int reply = ftpclient.getReplyCode();    
  89.             if (!FTPReply.isPositiveCompletion(reply)) {    
  90.                 //连接错误    
  91.                 Exception ee = new Exception("Can't Connect to :" + host);    
  92.                 throw ee;    
  93.             }    
  94.                 
  95.             //登录    
  96.             if (ftpclient.login(user, password) == false) {    
  97.                 // invalid user/password    
  98.                 Exception ee = new Exception("Invalid user/password");    
  99.                 throw ee;    
  100.             }    
  101.     
  102.             //设置传送模式   
  103.             ftpclient.setFileType(FTP.BINARY_FILE_TYPE);    
  104.                 
  105.             // 取得文件    
  106.             ftpclient.retrieveFile(remoteFilename, os);    
  107.     
  108.         } catch (IOException e) {    
  109.             throw e;    
  110.         } finally {    
  111.             try {    
  112.                 ftpclient.disconnect(); //解除连接   
  113.             } catch (IOException e) {    
  114.             }    
  115.         }    
  116.     }    
  117. }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值