Java实现ftp上传文件、文件夹

来源:互联网 发布:约瑟夫问题 c语言 编辑:程序博客网 时间:2024/05/17 05:46
[java] view plain copy
  1. import java.io.File;    
  2. import java.io.FileInputStream;    
  3. import org.apache.commons.net.ftp.FTPClient;    
  4. import org.apache.commons.net.ftp.FTPReply;    
  5.     
  6. public class test {      
  7.        
  8.     private  FTPClient ftp;      
  9.     /**  
  10.      *   
  11.      * @param path 上传到ftp服务器哪个路径下     
  12.      * @param addr 地址  
  13.      * @param port 端口号  
  14.      * @param username 用户名  
  15.      * @param password 密码  
  16.      * @return  
  17.      * @throws Exception  
  18.      */    
  19.     private  boolean connect(String path,String addr,int port,String username,String password) throws Exception {      
  20.         boolean result = false;      
  21.         ftp = new FTPClient();      
  22.         int reply;      
  23.         ftp.connect(addr,port);      
  24.         ftp.login(username,password);      
  25.         ftp.setFileType(FTPClient.BINARY_FILE_TYPE);      
  26.         reply = ftp.getReplyCode();      
  27.         if (!FTPReply.isPositiveCompletion(reply)) {      
  28.             ftp.disconnect();      
  29.             return result;      
  30.         }      
  31.         ftp.changeWorkingDirectory(path);      
  32.         result = true;      
  33.         return result;      
  34.     }      
  35.     /**  
  36.      *   
  37.      * @param file 上传的文件或文件夹  
  38.      * @throws Exception  
  39.      */    
  40.     private void upload(File file) throws Exception{      
  41.         if(file.isDirectory()){           
  42.             ftp.makeDirectory(file.getName());                
  43.             ftp.changeWorkingDirectory(file.getName());      
  44.             String[] files = file.list();             
  45.             for (int i = 0; i < files.length; i++) {      
  46.                 File file1 = new File(file.getPath()+"\\"+files[i] );      
  47.                 if(file1.isDirectory()){      
  48.                     upload(file1);      
  49.                     ftp.changeToParentDirectory();      
  50.                 }else{                    
  51.                     File file2 = new File(file.getPath()+"\\"+files[i]);      
  52.                     FileInputStream input = new FileInputStream(file2);      
  53.                     ftp.storeFile(file2.getName(), input);      
  54.                     input.close();                            
  55.                 }                 
  56.             }      
  57.         }else{      
  58.             File file2 = new File(file.getPath());      
  59.             FileInputStream input = new FileInputStream(file2);      
  60.             ftp.storeFile(file2.getName(), input);      
  61.             input.close();        
  62.         }      
  63.     }      
  64.    public static void main(String[] args) throws Exception{    
  65.       test t = new test();    
  66.       t.connect("""localhost"21"yhh""yhhazr");    
  67.       File file = new File("e:\\uploadify");    
  68.       t.upload(file);    
  69.    }    
  70. }    
文章来源:http://blog.csdn.net/yhhazr/article/details/7431037
0 0
原创粉丝点击