文件上传至FTP工具类

package com.free.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * 文件上传到FTP服务器
 * @author guzai
 * @since  2014-09-10
 * 本地可用(serv-u建立FTP服务器)
 */
public class FtpUtil {
	private  static FTPClient ftp;    
	
    /**    
     * 连接FTP服务器
     * <hr>
     * @param path 上传到ftp服务器哪个路径下
     * @param addr 地址    
     * @param port 端口号    
     * @param username 用户名    
     * @param password 密码    
     * @return    
     */      
    public static boolean connect(String path,String addr,int port,String username,String password) throws Exception {        
    	int reply;        
        boolean result = false;        
        ftp = new FTPClient();
        //设置编码
        ftp.setControlEncoding("UTF-8");
        //连接ftp
        ftp.connect(addr,port);
        //登录
        boolean flag = ftp.login(username,password);
        if(!flag){
        	return false;
        }else{
        	ftp.setFileType(FTPClient.BINARY_FILE_TYPE);        
        	reply = ftp.getReplyCode();        
        	if (!FTPReply.isPositiveCompletion(reply)) {        
        		ftp.disconnect();        
        		return result;        
        	}        
        	ftp.changeWorkingDirectory(path);        
        	result = true;        
        	return result;        
        }
    }        
      
      
      
    /**
     * 上传文件到FTP
     * <hr>  
     * @param file 上传的文件或文件夹  
     * @param path 上传的文件的路径   
     */  
    public static void upload(File file , String path) {     
        boolean flag = false;
        try {
        	//文件夹
			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, path);
						ftp.changeToParentDirectory();
					} else {
						File file2 = new File(file.getPath() + "\\" + files[i]);
						FileInputStream input = new FileInputStream(file2);
						flag = ftp.storeFile(file2.getName(), input);
						input.close();
					}
				}
			} else {
				File file2 = new File(file.getPath());

				InputStream input = new FileInputStream(file2);
				ftp.changeWorkingDirectory(path);
				flag = ftp.storeFile(file2.getName(), input);

				input.close(); //关闭输入流  
				ftp.logout(); //退出连接  
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
        
		if(flag){
        	System.out.println("上传成功");
        }else{
        	System.out.println("上传失败");
        	
        }
    }      
      
      
         
    /**  
     * FTP下载文件
     * <hr>
     * @Description : FPT 下载文件方法  
     * @param downloadPath 下载文件的路径   
     * @param fileName  下载的文件名   
     * @param localPath 下载的文件本地路径  
     */  
    public static void download(String downloadPath , String fileName , String localPath  ){     
               
            boolean flag = false;
            try {
				ftp.changeWorkingDirectory(downloadPath);
				// 列出该目录下所有文件  
				FTPFile[] fs = ftp.listFiles();
				// 遍历所有文件,找到指定的文件  
				for (FTPFile ff : fs) {
					if (ff.getName().equals(fileName)) {
						// 根据绝对路径初始化文件  
						File localFile = new File(localPath + "/"
								+ ff.getName());
						// 输出流  
						OutputStream is = new FileOutputStream(localFile);
						// 下载文件  
						flag = ftp.retrieveFile(ff.getName(), is);
						is.close();
					}
				}
				ftp.logout();  //退出连接  
			} catch (Exception e) {
				e.printStackTrace();
			}
            
            if(flag){
            	System.out.println("下载成功!");  
            }else{
            	System.out.println("下载失败!");  
            }
    }
    
    /**
     * 测试
     */
    public static void main(String[] args) throws Exception{      
        
       /* boolean flag = FtpUtil.connect("D:\\", "127.0.0.1", 21, "guzai", "guzai");   
        if(flag){
        	//上传  
        	File file = new File("d:\\44.jpg");    
        	FtpUtil.upload(file , "F:\\FTP");  
        	//下载  
        	//t.download("F:\\FTP", "我创建的文件.txt", "D:\\");  
        }*/
        
        boolean flag = FtpUtil.connect("test\\", "192.168.101.97", 33, "ewaytec\\daemon_work", "12#$abCD");   
        if(flag){
        	File file = new File("d:\\44.jpg"); 
        	//上传  
        	FtpUtil.upload(file , "192.168.101.97\\test");  
        	//下载  
        	//t.download("F:\\FTP", "我创建的文件.txt", "D:\\");  
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值