• JAVA FTP 客户端 .


    • private FTPClient connectFtp(){  
    •     FTPClient ftp = null;  
    •     String user = "";  
    •     String password = "";  
    •     String server = "";  
    •     int port = "21";  
    •     String root = "/";  
    •       
    •     ftp=new FTPClient();  
    •     ftp.addProtocolCommandListener(new ProtocolCommandListener() {  
    •         String message = "";  
    •         public void protocolCommandSent(ProtocolCommandEvent event) {  
    •             message = event.getMessage();  
    •             if ("pass".equalsIgnoreCase(event.getCommand())) {  
    •                 // 隐藏密码   
    •                 message = message.substring(0, message.indexOf(" ")) + " ******";  
    •             }  
    •             System.out.println(message);  
    •         }  
    •         public void protocolReplyReceived(ProtocolCommandEvent event) {  
    •             System.out.println(event.getMessage());  
    •         }  
    •     });  
    •     try {  
    •         int reply;  
    •         ftp.connect(server, port);  
    •           
    •         reply = ftp.getReplyCode();  
    •         System.out.println("reply code: "+reply);  
    •         String msg;  
    •         if (!FTPReply.isPositiveCompletion(reply)) {  
    •             ftp.disconnect();  
    •             msg="FTP服务器拒绝访问";  
    •             System.out.println(msg);                  
    •         }  
    •         if (!ftp.login(user, password)) {  
    •             ftp.logout();  
    •             msg="用户名或者密码错误";  
    •             System.out.println(msg);              
    •         }  
    •           
    •         //设置ftp的工作目录   
    •         String[] folders = root.split("/");  
    •         for (int i = 0; i < folders.length; i++) {  
    •             String folder=folders[i];  
    •             if(folder.trim().length()==0)  
    •                 continue;  
    •               
    •             if (!ftp.changeWorkingDirectory(folder)) {  
    •                 if (ftp.makeDirectory(folder)) {  
    •                     if (!ftp.changeWorkingDirectory(folder)) {  
    •                         msg="不能转到" + folder + "目录";  
    •                         System.out.println(msg);                      
    •                     }  
    •                 } else {  
    •                     msg="不能在ftp服务器上建立" + folder + "目录";  
    •                     System.out.println(msg);  
    •                 }  
    •             }  
    •         }  
    •         System.out.println("Current working directory: "+ftp.printWorkingDirectory());            
    •     } catch (IOException e) {  
    •         e.printStackTrace();  
    •         System.out.println("connectFtp发生异常"+ e.getMessage());  
    •         disconnectFtp(ftp);  
    •     }  
    •     return ftp;  
    • }  
    •   
    • private void disconnectFtp(FTPClient ftp ){  
    •     if(ftp!=null && ftp.isConnected()){  
    •         try {  
    •             ftp.disconnect();  
    •         } catch (IOException e) {  
    •         }  
    •     }  
    • }  
    •   
    • private void uploadFile(File file, String fileAlias,FTPClient ftp) {  
    •     String filename=fileAlias;        
    •     try {  
    •         filename = new String(filename.getBytes(), "iso8859_1");  
    •     } catch (UnsupportedEncodingException e) {  
    •         filename=fileAlias;  
    •     }  
    •       
    •     InputStream istream=null;         
    •     try {  
    •         String msg;  
    •         System.out.println("Working Directory: "+ftp.printWorkingDirectory());  
    •         ftp.setFileType(FTP.BINARY_FILE_TYPE);  
    •         ftp.setControlEncoding("GBK");  
    •         istream = new FileInputStream(file);  
    •         ftp.storeFile(filename, istream);  
    •           
    •     } catch (IOException e) {  
    •         e.printStackTrace();  
    •         System.out.println("uploadFile()发生异常"+ e.getMessage());  
    •           
    •     } finally {  
    •         if(istream!=null)  
    •             try {  
    •                 istream.close();  
    •             } catch (IOException e1) {  
    •             }  
    •     }             
  • 相关阅读:
    【二分图】HEOI2012 朋友圈
    【转载】动态规划—各种 DP 优化
    【默哀】京阿尼纵火案一周年
    【暑假集训】HZOI2019 Luogu P1006 传纸条 二三四维解法
    【暑假集训】HZOI2019 水站 多种解法
    最小二乘法求线性回归方程
    51Nod 最大M子段和系列 V1 V2 V3
    【博弈论】51Nod 1534 棋子游戏
    【最短路】CF 938D Buy a Ticket
    51nod1524 最大子段和V2
  • 原文地址:https://www.cnblogs.com/anuoruibo/p/2767203.html
Copyright © 2020-2023  润新知