【javascript学习指南】java ftp 上传一个文件目录的例子

更新时间:2020-11-26    来源:js教程    手机版     字体:

【www.bbyears.com--js教程】

java ftp 上传一个文件目录

package yq1012;
import java.io.File; 
import java.io.FileInputStream; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPReply; 
 
public class test {   
    
    private  FTPClient ftp;   
    /**
     * 
     * @param path 上传到ftp服务器哪个路径下   
     * @param addr 地址
     * @param port 端口号
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws Exception
     */ 
    private  boolean connect(String path,String addr,int port,String username,String password) throws Exception {   
        boolean result = false;   
        ftp = new FTPClient();   
        int reply;   
        ftp.connect(addr,port);   
        ftp.login(username,password);   
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);   
        reply = ftp.getReplyCode();   
        if (!FTPReply.isPositiveCompletion(reply)) {   
            ftp.disconnect();   
            return result;   
        }   
        ftp.changeWorkingDirectory(path);   
        result = true;   
        return result;   
    }   
    /**
     * 
     * @param file 上传的文件或文件夹
     * @throws Exception
     */ 
    private void upload(File file) throws Exception{   
        if(file.isDirectory()){        
            ftp.makeDirectory(file.getName());             
            ftp.changeWorkingDirectory(file.getName());   
            String[] files = file.list();          
            for (int i = 0; i < files.length; i++) {   
                File file1 = new File(file.getPath()+"\\"+files[i] );   
                if(file1.isDirectory()){   
                    upload(file1);   
                    ftp.changeToParentDirectory();   
                }else{                 
                    File file2 = new File(file.getPath()+"\\"+files[i]);   
                    FileInputStream input = new FileInputStream(file2);   
                    ftp.storeFile(file2.getName(), input);   
                    input.close();                         
                }              
            }   
        }else{   
            File file2 = new File(file.getPath());   
            FileInputStream input = new FileInputStream(file2);   
            ftp.storeFile(file2.getName(), input);   
            input.close();     
        }   
    }   
   public static void main(String[] args) throws Exception{ 
  String url = "www.111cn.net";
  String username = "user";
  String password = "password";
 
      test t = new test(); 
      t.connect("", url, 21, username, password); 
      File file = new File("D:\\XINXI"); 
      t.upload(file); 
   } 
}

本文来源:http://www.bbyears.com/wangyezhizuo/112845.html

热门标签

更多>>

本类排行