• ftp uploadFileAction(重要)


      TelnetOUtputStream os = ftpClient.put(filename);

    File file_in = new File(localPath);

    FileInputStream is = new FileInputStream(file_in);

    byte[] bytes = new byte[1024];

    1. /** 
    2.  * FTP文件上传与下载 
    3.  * notice: 
    4.  * 之所以每次都要连接一次ftp是让它的目录重新返回到相对的根目录, 
    5.  * 如果复用上次的FtpClient则可能它当前在FTP的目录不是我们想要的 
    6.  * 目录,所以在FTP上传下载文件时,最好每次都重新登录一下FTP 
    7.  * @author lgh 
    8.  */  
    9. public class FTPClient {  
    10.   
    11.     private FtpClient ftpClient;  
    12.     private String ip;  
    13.     private int port;  
    14.     private String username;  
    15.     private String password;  
    16.   
    17.     public FTPClient() {  
    18.     }  
    19.   
    20.     public FTPClient(String ip, int port, String username, String password) {  
    21.         this.ip = ip;  
    22.         this.port = port;  
    23.         this.username = username;  
    24.         this.password = password;  
    25.     }  
    26.   
    27.     /** 
    28.      * 需要备份的文件 
    29.      * @param list 
    30.      * @return 
    31.      */  
    32.     private List needBackFile(List list, String relativeName) {  
    33.         List fileNames = new ArrayList();  
    34.         for (int i = 0; i < list.size(); i++) {  
    35.             String temp = (String) list.get(i);  
    36.             if (temp.indexOf(relativeName) > 0) {  
    37.                 fileNames.add(temp);  
    38.             }  
    39.         }  
    40.         return fileNames;  
    41.     }  
    42.   
    43.     public static void main(String[] args) {  
    44.         FTPClient client = new FTPClient(".....", 21, "...", "....");  
    45.   
    46.         try {  
    47. //            client.downloadFile("CRM/ccbcrm/", "D://", "CRMClientLog.log", "CRMClientLog.log");  
    48. //            client.uploadFile("", "D://", "CRMClientLog.log");  
    49.             List list = client.getList("csrtestftp/网络/", false);  
    50.         } catch (Exception ex) {  
    51.             Logger.getLogger(FTPClient.class.getName()).log(Level.SEVERE, null, ex);  
    52.         }  
    53.     }  
    54.   
    55.     /** 
    56.      * 关闭FTP连接  
    57.      * @throws java.lang.Exception 
    58.      */  
    59.     public void closeServer() throws Exception {  
    60.         ftpClient.closeServer();  
    61.     }  
    62.   
    63.     /** 
    64.      * 连接ftp服务器 
    65.      * @param ip 
    66.      * @param port 
    67.      * @param user 
    68.      * @param pwd 
    69.      * @return 
    70.      * @throws Exception 
    71.      */  
    72.     public boolean connectServer(String ip, int port, String user, String pwd)  
    73.             throws Exception {  
    74.         boolean isSuccess = false;  
    75.         try {  
    76.             ftpClient = new FtpClient();  
    77.             ftpClient.openServer(ip, port);  
    78.             ftpClient.login(user, pwd);  
    79.             isSuccess = true;  
    80.         } catch (Exception ex) {  
    81.             ex.printStackTrace();  
    82.         }  
    83.         return isSuccess;  
    84.     }  
    85.   
    86.     /** 
    87.      * 获得远程下的目录 
    88.      * @param remotePath 远程目录 
    89.      * @param fullPath 是否需要完整路径 
    90.      * @return 
    91.      */  
    92.     public List getList(String remotePath, boolean fullPath) {  
    93.         List list = new ArrayList();  
    94.         try {  
    95.             if (connectServer(ip, port, username, password)) {  
    96.                 BufferedReader br = new BufferedReader(new InputStreamReader(ftpClient.nameList(remotePath)));  
    97.                 String temp = ftpClient.getResponseString();  
    98.                 System.out.println(temp);  
    99.                 String readLine = null;  
    100.                 int lastIndex;  
    101.                 if ((lastIndex = remotePath.lastIndexOf("/")) > 0||(lastIndex = remotePath.lastIndexOf("//")) > 0  
    102.                     ||(lastIndex = remotePath.lastIndexOf("\")) > 0||(lastIndex = remotePath.lastIndexOf(File.separator)) > 0) {  
    103.                     remotePath = remotePath.substring(0, lastIndex);  
    104.                 }   //去掉remotePath的后缀,可能是'/',也有可能是其他符号  
    105.                 while ((readLine = br.readLine()) != null) {  
    106.   
    107.                     if (!fullPath) {  
    108.                         list.add(readLine.substring(remotePath.length() + 1, readLine.length()));  
    109.                         System.out.println(readLine.substring(remotePath.length() + 1, readLine.length()));  
    110.                     } else {  
    111.                         list.add(readLine);  
    112.                         System.out.println(readLine);  
    113.                     }  
    114.                 }  
    115.                 ftpClient.closeServer();  
    116.             }  
    117.         } catch (Exception ex) {  
    118.             ex.printStackTrace();  
    119.         }  
    120.         return list;  
    121.     }  
    122.   
    123.     /** 
    124.      * 下载文件 
    125.      * @param remotePath 
    126.      * @param localPath 
    127.      * @param filename 
    128.      * @throws Exception 
    129.      */  
    130.     public void downloadFile(String remotePath, String localPath, String remoteFileName, String localFileName) throws Exception {  
    131.         try {  
    132.             if (connectServer(ip, port, username, password)) {  
    133.                 if (remotePath.length() != 0) {  
    134.                     ftpClient.cd(remotePath);  
    135.                 }  
    136.                 ftpClient.binary();  
    137.                 TelnetInputStream is = ftpClient.get(remoteFileName);  
    138.                 File fp = new File(localPath);  
    139.                 if (!fp.exists()) {  
    140.                     fp.mkdirs();  
    141.                 }  
    142.                 File file_out = new File(localPath + File.separator + localFileName);  
    143.                 FileOutputStream os = new FileOutputStream(file_out);  
    144.                 byte[] bytes = new byte[1024];  
    145.                 int readBye;  
    146.                 while ((readBye = is.read(bytes)) != -1) {  
    147.                     os.write(bytes, 0, readBye);  
    148.                 }  
    149.                 is.close();  
    150.                 os.close();  
    151.                 ftpClient.closeServer();  
    152.             }  
    153.         } catch (Exception ex) {  
    154.             ex.printStackTrace();  
    155.         }  
    156.     }  
    157.   
    158.     /** 
    159.      * 上传文件 
    160.      * @param remotePath 
    161.      * @param localPath 
    162.      * @param filename 
    163.      * @throws Exception 
    164.      */  
    165.     public void uploadFile(String remotePath, String localPath, String filename) throws Exception {  
    166.         try {  
    167.             if (connectServer(ip, port, username, password)) {  
    168.                 if (remotePath.length() != 0) {  
    169.                     ftpClient.cd(remotePath);  
    170.                 }  
    171.                 ftpClient.binary();  
    172.                 TelnetOutputStream os = ftpClient.put(filename);  
    173.                 File file_in = new File(localPath + File.separator + filename);  
    174.                 FileInputStream is = new FileInputStream(file_in);  
    175.                 byte[] bytes = new byte[1024];  
    176.                 int readBye;  
    177.                 while ((readBye = is.read(bytes)) != -1) {  
    178.                     os.write(bytes, 0, readBye);  
    179.                 }  
    180.                 is.close();  
    181.                 os.close();  
    182.                 ftpClient.closeServer();  
    183.             }  
    184.         } catch (Exception ex) {  
    185.             ex.printStackTrace();  
    186.         }  
    187.     }  
    188.   
    189.     /** 
    190.      * @return the ip 
    191.      */  
    192.     public String getIp() {  
    193.         return ip;  
    194.     }  
    195.   
    196.     /** 
    197.      * @param ip the ip to set 
    198.      */  
    199.     public void setIp(String ip) {  
    200.         this.ip = ip;  
    201.     }  
    202.   
    203.     /** 
    204.      * @return the port 
    205.      */  
    206.     public int getPort() {  
    207.         return port;  
    208.     }  
    209.   
    210.     /** 
    211.      * @param port the port to set 
    212.      */  
    213.     public void setPort(int port) {  
    214.         this.port = port;  
    215.     }  
    216.   
    217.     /** 
    218.      * @return the username 
    219.      */  
    220.     public String getUsername() {  
    221.         return username;  
    222.     }  
    223.   
    224.     /** 
    225.      * @param username the username to set 
    226.      */  
    227.     public void setUsername(String username) {  
    228.         this.username = username;  
    229.     }  
    230.   
    231.     /** 
    232.      * @return the password 
    233.      */  
    234.     public String getPassword() {  
    235.         return password;  
    236.     }  
    237.   
    238.     /** 
    239.      * @param password the password to set 
    240.      */  
    241.     public void setPassword(String password) {  
    242.         this.password = password;  
    243.     }  
    244. }  
  • 相关阅读:
    【XSY2534】【CF835D】Palindromic characteristics 回文自动机
    启发式合并&线段树合并/分裂&treap合并&splay合并
    【XSY2534】【BZOJ4817】树点涂色 LCT 倍增 线段树 dfs序
    线性求逆元
    l1 和l2范数的真实意义
    方向导数及梯度
    大厂实习总结和反思
    高考报考以及心态调整健康贴士
    【骑士走棋盘】
    【老鼠走迷宫一】
  • 原文地址:https://www.cnblogs.com/yelongsan/p/6400268.html
Copyright © 2020-2023  润新知