zoukankan      html  css  js  c++  java
  • 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) {  
    •             }  
    •     }             
  • 相关阅读:
    ASP扫盲学习班第六课 添加新保存的数据
    三级联动菜单免刷新加载
    ASP按定制格式导出word文件的完美解决思路
    通用客户资源管理系统(客户资料保护库)
    SmR 通用信息采集系统(新闻小偷)
    ASP扫盲学习班第三课 程序界面的设计
    我的新作品(单身贵族网全站)
    《将博客搬至CSDN》
    我的新作品
    asp函数大全
  • 原文地址:https://www.cnblogs.com/anuoruibo/p/2767203.html
Copyright © 2011-2022 走看看