zoukankan      html  css  js  c++  java
  • 使用sun.net.ftp下载、上传文件完整版

    复制代码

    1. package com.northking.dataManager.util;   
    2. import sun.net.ftp.*;   
    3. import sun.net.*;   
    4. import java.io.*;   
    5.   
    6. /**  
    7.  * 使用sun.net.ftp工具包进行ftp上传下载  
    8.  * @author maochangming  
    9.  * @date 2008-6-20  13:09:29  
    10.  * @description:  
    11.  */  
    12. public class FtpTool {   
    13.     String ip;   
    14.     int port;   
    15.     String user;   
    16.     String pwd;   
    17.     String remotePath;   
    18.     String localPath;   
    19.     FtpClient ftpClient;   
    20.   
    21.     /**  
    22.      * 连接ftp服务器  
    23.      * @param ip  
    24.      * @param port  
    25.      * @param user  
    26.      * @param pwd  
    27.      * @return  
    28.      * @throws Exception  
    29.      */  
    30.     public boolean connectServer(String ip, int port, String user, String pwd)   
    31.         throws Exception {   
    32.         boolean isSuccess = false;   
    33.         try {   
    34.             ftpClient = new FtpClient();   
    35.             ftpClient.openServer(ip, port);   
    36.             ftpClient.login(user, pwd);   
    37.             isSuccess = true;   
    38.         } catch (Exception ex) {   
    39.             throw new Exception("Connect ftp server error:" + ex.getMessage());   
    40.         }   
    41.         return isSuccess;   
    42.     }   
    43.   
    44.     /**  
    45.      * 下载文件  
    46.      * @param remotePath  
    47.      * @param localPath  
    48.      * @param filename  
    49.      * @throws Exception  
    50.      */  
    51.     public void downloadFile(String remotePath,String localPath, String filename) throws Exception {   
    52.         try {   
    53.             if (connectServer(getIp(), getPort(), getUser(), getPwd())) {   
    54.                 if (remotePath.length() != 0)   
    55.                     ftpClient.cd(remotePath);   
    56.                 ftpClient.binary();   
    57.                 TelnetInputStream is = ftpClient.get(filename);   
    58.                 File file_out = new File(localPath + File.separator + filename);   
    59.                 FileOutputStream os = new FileOutputStream(file_out);   
    60.                 byte[] bytes = new byte[1024];   
    61.                 int c;   
    62.                 while ((c = is.read(bytes)) != -1) {   
    63.                     os.write(bytes, 0, c);   
    64.                 }   
    65.                 is.close();   
    66.                 os.close();   
    67.                 ftpClient.closeServer();   
    68.             }   
    69.         } catch (Exception ex) {   
    70.             throw new Exception("ftp download file error:" + ex.getMessage());   
    71.         }   
    72.     }   
    73.   
    74.     /**  
    75.      * 上传文件  
    76.      * @param remotePath  
    77.      * @param localPath  
    78.      * @param filename  
    79.      * @throws Exception  
    80.      */  
    81.     public void uploadFile(String remotePath,String localPath, String filename) throws Exception {   
    82.         try {   
    83.             if (connectServer(getIp(), getPort(), getUser(), getPwd())) {   
    84.                 if (remotePath.length() != 0)   
    85.                     ftpClient.cd(remotePath);   
    86.                 ftpClient.binary();   
    87.                 TelnetOutputStream os = ftpClient.put(filename);   
    88.                 File file_in = new File(localPath + File.separator + filename);   
    89.                 FileInputStream is = new FileInputStream(file_in);   
    90.                 byte[] bytes = new byte[1024];   
    91.                 int c;   
    92.                 while ((c = is.read(bytes)) != -1) {   
    93.                     os.write(bytes, 0, c);   
    94.                 }   
    95.                 is.close();   
    96.                 os.close();   
    97.                 ftpClient.closeServer();   
    98.             }   
    99.         } catch (Exception ex) {   
    100.             throw new Exception("ftp upload file error:" + ex.getMessage());   
    101.         }   
    102.     }   
    103.   
    104.     /**  
    105.      * @return  
    106.      */  
    107.     public String getIp() {   
    108.         return ip;   
    109.     }   
    110.   
    111.     /**  
    112.      * @return  
    113.      */  
    114.     public int getPort() {   
    115.         return port;   
    116.     }   
    117.   
    118.     /**  
    119.      * @return  
    120.      */  
    121.     public String getPwd() {   
    122.         return pwd;   
    123.     }   
    124.   
    125.     /**  
    126.      * @return  
    127.      */  
    128.     public String getUser() {   
    129.         return user;   
    130.     }   
    131.   
    132.     /**  
    133.      * @param string  
    134.      */  
    135.     public void setIp(String string) {   
    136.         ip = string;   
    137.     }   
    138.   
    139.     /**  
    140.      * @param i  
    141.      */  
    142.     public void setPort(int i) {   
    143.         port = i;   
    144.     }   
    145.   
    146.     /**  
    147.      * @param string  
    148.      */  
    149.     public void setPwd(String string) {   
    150.         pwd = string;   
    151.     }   
    152.   
    153.     /**  
    154.      * @param string  
    155.      */  
    156.     public void setUser(String string) {   
    157.         user = string;   
    158.     }   
    159.   
    160.     /**  
    161.      * @return  
    162.      */  
    163.     public FtpClient getFtpClient() {   
    164.         return ftpClient;   
    165.     }   
    166.   
    167.     /**  
    168.      * @param client  
    169.      */  
    170.     public void setFtpClient(FtpClient client) {   
    171.         ftpClient = client;   
    172.     }   
    173.   
    174.     /**  
    175.      * @return  
    176.      */  
    177.     public String getRemotePath() {   
    178.         return remotePath;   
    179.     }   
    180.   
    181.     /**  
    182.      * @param string  
    183.      */  
    184.     public void setRemotePath(String string) {   
    185.         remotePath = string;   
    186.     }   
    187.   
    188.     /**  
    189.      * @return  
    190.      */  
    191.     public String getLocalPath() {   
    192.         return localPath;   
    193.     }   
    194.   
    195.     /**  
    196.      * @param string  
    197.      */  
    198.     public void setLocalPath(String string) {   
    199.         localPath = string;   
    200.     }   
    201.   
    202. }  
    package com.northking.dataManager.util;import sun.net.ftp.*;import sun.net.*;import java.io.*;/** * 使用sun.net.ftp工具包进行ftp上传下载 * @author maochangming * @date 2008-6-20  13:09:29 * @description: */public class FtpTool {String ip;int port;String user;String pwd;String remotePath;String localPath;FtpClient ftpClient;/** * 连接ftp服务器 * @param ip * @param port * @param user * @param pwd * @return * @throws Exception */public boolean connectServer(String ip, int port, String user, String pwd)throws Exception {boolean isSuccess = false;try {ftpClient = new FtpClient();ftpClient.openServer(ip, port);ftpClient.login(user, pwd);isSuccess = true;} catch (Exception ex) {throw new Exception("Connect ftp server error:" + ex.getMessage());}return isSuccess;}/** * 下载文件 * @param remotePath * @param localPath * @param filename * @throws Exception */public void downloadFile(String remotePath,String localPath, String filename) throws Exception {try {if (connectServer(getIp(), getPort(), getUser(), getPwd())) {if (remotePath.length() != 0)ftpClient.cd(remotePath);ftpClient.binary();TelnetInputStream is = ftpClient.get(filename);File file_out = new File(localPath + File.separator + filename);FileOutputStream os = new FileOutputStream(file_out);byte[] bytes = new byte[1024];int c;while ((c = is.read(bytes)) != -1) {os.write(bytes, 0, c);}is.close();os.close();ftpClient.closeServer();}} catch (Exception ex) {throw new Exception("ftp download file error:" + ex.getMessage());}}/** * 上传文件 * @param remotePath * @param localPath * @param filename * @throws Exception */public void uploadFile(String remotePath,String localPath, String filename) throws Exception {try {if (connectServer(getIp(), getPort(), getUser(), getPwd())) {if (remotePath.length() != 0)ftpClient.cd(remotePath);ftpClient.binary();TelnetOutputStream os = ftpClient.put(filename);File file_in = new File(localPath + File.separator + filename);FileInputStream is = new FileInputStream(file_in);byte[] bytes = new byte[1024];int c;while ((c = is.read(bytes)) != -1) {os.write(bytes, 0, c);}is.close();os.close();ftpClient.closeServer();}} catch (Exception ex) {throw new Exception("ftp upload file error:" + ex.getMessage());}}/** * @return */public String getIp() {return ip;}/** * @return */public int getPort() {return port;}/** * @return */public String getPwd() {return pwd;}/** * @return */public String getUser() {return user;}/** * @param string */public void setIp(String string) {ip = string;}/** * @param i */public void setPort(int i) {port = i;}/** * @param string */public void setPwd(String string) {pwd = string;}/** * @param string */public void setUser(String string) {user = string;}/** * @return */public FtpClient getFtpClient() {return ftpClient;}/** * @param client */public void setFtpClient(FtpClient client) {ftpClient = client;}/** * @return */public String getRemotePath() {return remotePath;}/** * @param string */public void setRemotePath(String string) {remotePath = string;}/** * @return */public String getLocalPath() {return localPath;}/** * @param string */public void setLocalPath(String string) {localPath = string;}}



    Junit测试类

    Java代码 复制代码
    1. package com.northking.dataManager.dataimport.parse.test;   
    2.   
    3. import com.northking.dataManager.util.FtpTool;   
    4.   
    5. import junit.framework.TestCase;   
    6.   
    7. /**  
    8.  * @author maochangming  
    9.  * @date 2008-6-20  13:09:11  
    10.  * @description:  
    11.  */  
    12. public class FtpToolTest extends TestCase {   
    13.     FtpTool ftpTool;   
    14.   
    15.     /**  
    16.      * Constructor for FtpToolTest.  
    17.      * @param arg0  
    18.      */  
    19.     public FtpToolTest(String arg0) {   
    20.         super(arg0);   
    21.     }   
    22.   
    23.     public static void main(String[] args) {   
    24.         junit.textui.TestRunner.run(FtpToolTest.class);   
    25.     }   
    26.        
    27.     public void testDownLoadFile()throws Exception{   
    28.         ftpTool.downloadFile(ftpTool.getRemotePath(),"c:/downloads","JBFImgMng.CAB");   
    29.     }   
    30.   
    31.     /*  
    32.      * @see TestCase#setUp()  
    33.      */  
    34.     protected void setUp() throws Exception {   
    35.         ftpTool = new FtpTool();   
    36.         ftpTool.setIp("10.164.12.70");   
    37.         ftpTool.setPort(2100);   
    38.         ftpTool.setUser("share");   
    39.         ftpTool.setPwd("share");   
    40.         ftpTool.setRemotePath("/paeams");   
    41.         super.setUp();   
    42.     }   
    43.   
    44.     /*  
    45.      * @see TestCase#tearDown()  
    46.      */  
    47.     protected void tearDown() throws Exception {   
    48.         super.tearDown();   
    49.     }   
    50.   
    51. }  
  • 相关阅读:
    C# Nugut CsvHelper 使用
    C# 读写txt
    Js打开QQ聊天对话窗口
    Js 读写Cookies
    js 计算时间差
    C# 读取CSV文件
    使用 SqlBulkCopy 批量插入数据
    sql 添加列并设置默认值
    C# 获取Enum 描述和值集合
    SQL连接其它服务器操作
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/1867669.html
Copyright © 2011-2022 走看看