Jdk1.7自带的FTP文件上传下载

来源:互联网 发布:php面向对象操作数据库 编辑:程序博客网 时间:2024/05/15 06:40

Jdk1.7自带的FTP文件上传下载


Ftp相关的类库很多,这里以JDK自带的FTP上传下载功能为例。
Java代码  收藏代码
  1. package com.boonya.upload.util.simple;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.net.InetSocketAddress;  
  8. import sun.net.TelnetInputStream;  
  9. import sun.net.TelnetOutputStream;  
  10. import sun.net.ftp.FtpClient;  
  11. import sun.net.ftp.FtpProtocolException;  
  12. /** 
  13.  * Java自带的API对FTP的操作 
  14.  * @Jdk:version 1.7 
  15.  * @Title:Ftp.java 
  16.  * @author: boonya 
  17.  * @notice: 貌似此方法有个缺陷,不能操作大文件 
  18.  */  
  19. public class FtpJdk  
  20. {  
  21.     /** 
  22.      *  
  23.      * 本地文件名 
  24.      */  
  25.     private String localfilename;  
  26.     /** 
  27.      *  
  28.      * 远程文件名 
  29.      */  
  30.     private String remotefilename;  
  31.     /** 
  32.      *  
  33.      * FTP客户端 
  34.      */  
  35.     private FtpClient ftpClient;  
  36.   
  37.     /** 
  38.      * 服务器连接 
  39.      *  
  40.      * @param ip 
  41.      *            服务器IP 
  42.      * @param port 
  43.      *            服务器端口 
  44.      * @param user 
  45.      *            用户名 
  46.      * @param password 
  47.      *            密码 
  48.      * @param path 
  49.      *            服务器路径 
  50.      * @throws FtpProtocolException 
  51.      *  
  52.      */  
  53.     public void connectServer(String ip, int port, String user, String password, String path) throws FtpProtocolException  
  54.     {  
  55.         try  
  56.         {  
  57.             /* ******连接服务器的两种方法****** */  
  58.             // 第一种方法  
  59.             /* 
  60.              * ftpClient =FtpClient.create(); 
  61.              * ftpClient.connect(new InetSocketAddress(ip, port)); 
  62.              */  
  63.             // 第二种方法  
  64.             ftpClient = FtpClient.create(ip);  
  65.             ftpClient.login(user, null, password);  
  66.             // 设置成2进制传输  
  67.             ftpClient.setBinaryType();  
  68.             System.out.println("login success!");  
  69.   
  70.             if (path.length() != 0)  
  71.             {  
  72.                 // 把远程系统上的目录切换到参数path所指定的目录  
  73.                 ftpClient.changeDirectory(path);  
  74.             }  
  75.             ftpClient.setBinaryType();  
  76.         } catch (IOException ex)  
  77.         {  
  78.             ex.printStackTrace();  
  79.             throw new RuntimeException(ex);  
  80.         }  
  81.   
  82.     }  
  83.   
  84.     /** 
  85.      * 关闭连接 
  86.      *  
  87.      */  
  88.   
  89.     public void closeConnect()  
  90.     {  
  91.         try  
  92.         {  
  93.             ftpClient.close();  
  94.             System.out.println("disconnect success");  
  95.         } catch (IOException ex)  
  96.         {  
  97.             System.out.println("not disconnect");  
  98.             ex.printStackTrace();  
  99.             throw new RuntimeException(ex);  
  100.         }  
  101.     }  
  102.   
  103.     /** 
  104.      *  
  105.      * 上传文件 
  106.      *  
  107.      * @param localFile 
  108.      *            本地文件 
  109.      * @param remoteFile 
  110.      *            远程文件 
  111.      * @throws FtpProtocolException 
  112.      */  
  113.     public void upload(String localFile, String remoteFile) throws FtpProtocolException  
  114.     {  
  115.         this.localfilename = localFile;  
  116.         this.remotefilename = remoteFile;  
  117.         TelnetOutputStream os = null;  
  118.         FileInputStream is = null;  
  119.         try  
  120.         {  
  121.             // 将远程文件加入输出流中  
  122.             os = (TelnetOutputStream) ftpClient.putFileStream(this.remotefilename, true);  
  123.   
  124.             // 获取本地文件的输入流  
  125.             File file_in = new File(this.localfilename);  
  126.             is = new FileInputStream(file_in);  
  127.   
  128.             // 创建一个缓冲区  
  129.             byte[] bytes = new byte[1024];  
  130.             int c;  
  131.             while ((c = is.read(bytes)) != -1)  
  132.             {  
  133.                 os.write(bytes, 0, c);  
  134.             }  
  135.             System.out.println("upload success");  
  136.         } catch (IOException ex)  
  137.         {  
  138.             System.out.println("not upload");  
  139.             ex.printStackTrace();  
  140.             throw new RuntimeException(ex);  
  141.         } finally  
  142.         {  
  143.             try  
  144.             {  
  145.                 if (is != null)  
  146.                 {  
  147.                     is.close();  
  148.                 }  
  149.             } catch (IOException e)  
  150.             {  
  151.                 e.printStackTrace();  
  152.             } finally  
  153.             {  
  154.                 try  
  155.                 {  
  156.                     if (os != null)  
  157.                     {  
  158.                         os.close();  
  159.                     }  
  160.                 } catch (IOException e)  
  161.                 {  
  162.                     e.printStackTrace();  
  163.                 }  
  164.             }  
  165.         }  
  166.     }  
  167.   
  168.     /** 
  169.      *  
  170.      * 下载文件 
  171.      *  
  172.      * @param remoteFile 
  173.      *            远程文件路径(服务器端) 
  174.      * @param localFile 
  175.      *            本地文件路径(客户端) 
  176.      * @throws FtpProtocolException 
  177.      *  
  178.      */  
  179.     public void download(String remoteFile, String localFile) throws FtpProtocolException  
  180.     {  
  181.         TelnetInputStream is = null;  
  182.         FileOutputStream os = null;  
  183.         try  
  184.         {  
  185.   
  186.             // 获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。  
  187.             is = (TelnetInputStream) ftpClient.getFileStream(remoteFile);  
  188.             File file_in = new File(localFile);  
  189.             os = new FileOutputStream(file_in);  
  190.   
  191.             byte[] bytes = new byte[1024];  
  192.             int c;  
  193.             while ((c = is.read(bytes)) != -1)  
  194.             {  
  195.                 os.write(bytes, 0, c);  
  196.             }  
  197.             System.out.println("download success");  
  198.         } catch (IOException ex)  
  199.         {  
  200.             System.out.println("not download");  
  201.             ex.printStackTrace();  
  202.             throw new RuntimeException(ex);  
  203.         } finally  
  204.         {  
  205.             try  
  206.             {  
  207.                 if (is != null)  
  208.                 {  
  209.                     is.close();  
  210.                 }  
  211.             } catch (IOException e)  
  212.             {  
  213.                 e.printStackTrace();  
  214.             } finally  
  215.             {  
  216.                 try  
  217.                 {  
  218.                     if (os != null)  
  219.                     {  
  220.                         os.close();  
  221.                     }  
  222.                 } catch (IOException e)  
  223.                 {  
  224.                     e.printStackTrace();  
  225.                 }  
  226.             }  
  227.         }  
  228.     }  
  229.   
  230.     /** 
  231.      * 函数入口 
  232.      *  
  233.      * @param agrs 
  234.      */  
  235.     public static void main(String agrs[])  
  236.     {  
  237.   
  238.         String filepath[] =  
  239.         { "/temp/aa.txt""/temp/regist.log" };  
  240.         String localfilepath[] =  
  241.         { "C:\\tmp\\1.txt""C:\\tmp\\2.log" };  
  242.         FtpJdk ftp = new FtpJdk();  
  243.         /* 
  244.          * 使用默认的端口号、用户名、密码以及根目录连接FTP服务器 
  245.          */  
  246.         try  
  247.         {  
  248.             ftp.connectServer("127.0.0.1"22"boonya""user@""/temp");  
  249.         } catch (FtpProtocolException e)  
  250.         {  
  251.             e.printStackTrace();  
  252.         }  
  253.         // 下载  
  254.         for (int i = 0; i < filepath.length; i++)  
  255.         {  
  256.             try  
  257.             {  
  258.                 ftp.download(filepath[i], localfilepath[i]);  
  259.             } catch (FtpProtocolException e)  
  260.             {  
  261.                 e.printStackTrace();  
  262.             }  
  263.         }  
  264.         String localfile = "E:\\contact.txt";  
  265.         String remotefile = "/temp/records.txt";  
  266.         // 上传  
  267.         try  
  268.         {  
  269.             ftp.upload(localfile, remotefile);  
  270.         } catch (FtpProtocolException e)  
  271.         {  
  272.             e.printStackTrace();  
  273.         }  
  274.         ftp.closeConnect();  
  275.     }  
  276.   

0 0
原创粉丝点击