JDK1.7 连接FtpClient,上传下载

FTPUtil.java

[java]  view plain  copy
  1. package com.ftp;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.net.InetSocketAddress;  
  10. import java.net.SocketAddress;  
  11. import sun.net.ftp.FtpClient;  
  12. import sun.net.ftp.FtpProtocolException;  
  13.   
  14. public class FTPUtil {  
  15.     /** 
  16.      * 连接ftp服务器 JDK 1.7 
  17.      *  
  18.      * @param url 
  19.      * @param port 
  20.      * @param username 
  21.      * @param password 
  22.      * @return FtpClient 
  23.      * @throws FtpProtocolException 
  24.      * @throws IOException 
  25.      */  
  26.     public static FtpClient connectFTP(String url, int port, String username,  
  27.             String password) { // 创建ftp  
  28.         FtpClient ftp = null;  
  29.         try {  
  30.             // 创建地址  
  31.             SocketAddress addr = new InetSocketAddress(url, port);  
  32.             // 连接  
  33.             ftp = FtpClient.create();  
  34.             ftp.connect(addr);  
  35.             // 登陆  
  36.             ftp.login(username, password.toCharArray());  
  37.             ftp.setBinaryType();  
  38.             System.out.println(ftp.getWelcomeMsg());  
  39.         } catch (FtpProtocolException e) {  
  40.             // TODO Auto-generated catch block  
  41.             e.printStackTrace();  
  42.         } catch (IOException e) {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         }  
  46.         return ftp;  
  47.     }  
  48.   
  49.     /** 
  50.      * 切换目录 
  51.      *  
  52.      * @param ftp 
  53.      * @param path 
  54.      */  
  55.     public static void changeDirectory(FtpClient ftp, String path) {  
  56.         try {  
  57.             ftp.changeDirectory(path);  
  58.             System.out.println(ftp.getWorkingDirectory());  
  59.         } catch (FtpProtocolException e) {  
  60.             // TODO Auto-generated catch block  
  61.             e.printStackTrace();  
  62.         } catch (IOException e) {  
  63.             // TODO Auto-generated catch block  
  64.             e.printStackTrace();  
  65.         }  
  66.     }  
  67.   
  68.     /** 
  69.      * 关闭ftp 
  70.      *  
  71.      * @param ftp 
  72.      */  
  73.     public static void disconnectFTP(FtpClient ftp) {  
  74.         try {  
  75.             ftp.close();  
  76.             System.out.println("disconnect success!!");  
  77.         } catch (IOException e) {  
  78.             // TODO Auto-generated catch block  
  79.             e.printStackTrace();  
  80.         }  
  81.     }  
  82.   
  83.     /** 
  84.      * 上传文件 
  85.      *  
  86.      * @param localFile 
  87.      * @param ftpFile 
  88.      * @param ftp 
  89.      */  
  90.     public static void upload(String localFile, String ftpFile, FtpClient ftp) {  
  91.         OutputStream os = null;  
  92.         FileInputStream fis = null;  
  93.         try {  
  94.             // 将ftp文件加入输出流中。输出到ftp上  
  95.             os = ftp.putFileStream(ftpFile);  
  96.             File file = new File(localFile);  
  97.             // 创建一个缓冲区  
  98.             fis = new FileInputStream(file);  
  99.             byte[] bytes = new byte[1024];  
  100.             int c;  
  101.             while ((c = fis.read(bytes)) != -1) {  
  102.                 os.write(bytes, 0, c);  
  103.             }  
  104.             System.out.println("upload success!!");  
  105.         } catch (FtpProtocolException e) {  
  106.             // TODO Auto-generated catch block  
  107.             e.printStackTrace();  
  108.         } catch (IOException e) {  
  109.             // TODO Auto-generated catch block  
  110.             e.printStackTrace();  
  111.         } finally {  
  112.             try {  
  113.                 if (os != null)  
  114.                     os.close();  
  115.                 if (fis != null)  
  116.                     fis.close();  
  117.             } catch (IOException e) {  
  118.                 // TODO Auto-generated catch block  
  119.                 e.printStackTrace();  
  120.             }  
  121.         }  
  122.     }  
  123.   
  124.     /** 
  125.      * 文件下载 
  126.      *  
  127.      * @param localFile 
  128.      * @param ftpFile 
  129.      * @param ftp 
  130.      */  
  131.     public static void download(String localFile, String ftpFile, FtpClient ftp) {  
  132.         InputStream is = null;  
  133.         FileOutputStream fos = null;  
  134.         try {  
  135.             // 获取ftp上的文件  
  136.             is = ftp.getFileStream(ftpFile);  
  137.             File file = new File(localFile);  
  138.             byte[] bytes = new byte[1024];  
  139.             int i;  
  140.             fos = new FileOutputStream(file);  
  141.             while ((i = is.read(bytes)) != -1) {  
  142.                 fos.write(bytes, 0, i);  
  143.             }  
  144.             System.out.println("download success!!");  
  145.         } catch (FtpProtocolException e) {  
  146.             // TODO Auto-generated catch block  
  147.             e.printStackTrace();  
  148.         } catch (IOException e) {  
  149.             // TODO Auto-generated catch block  
  150.             e.printStackTrace();  
  151.         } finally {  
  152.             try {  
  153.                 if (fos != null)  
  154.                     fos.close();  
  155.                 if (is != null) {  
  156.                     is.close();  
  157.                 }  
  158.             } catch (IOException e) {  
  159.                 // TODO Auto-generated catch block  
  160.                 e.printStackTrace();  
  161.             }  
  162.         }  
  163.     }  
  164. }  

Test.java

[java]  view plain  copy
  1. package test;  
  2.   
  3. import java.net.InetAddress;  
  4. import java.net.UnknownHostException;  
  5. import sun.net.ftp.FtpClient;  
  6. import com.ftp.FTPUtil;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) {  
  10.         try {  
  11.             InetAddress addr = InetAddress.getLocalHost();  
  12.             System.out.println(addr.getHostAddress());  
  13.         } catch (UnknownHostException e) {  
  14.             // TODO Auto-generated catch block  
  15.             e.printStackTrace();  
  16.         }  
  17.         String ip = "10.10.10.10";  
  18.         int port = 21;  
  19.         String username = "root";  
  20.         String password = "root";  
  21.         String path = "/home";  
  22.         // 连接ftp  
  23.         FtpClient ftp = FTPUtil.connectFTP(ip, port, username, password);  
  24.         System.out.println(ftp.getWelcomeMsg());  
  25.         // 切换目录  
  26.         FTPUtil.changeDirectory(ftp, path);  
  27.         System.out.println("-----上传----");  
  28.         FTPUtil.upload("D:aaa.txt""/home/aaa.txt", ftp);  
  29.         System.out.println("-----下载----");  
  30.         FTPUtil.download("D:aaa.txt""/home/aaa.txt", ftp);  
  31.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值