FtpUtil

<!--FTP包-->
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.5</version>
</dependency>
package com.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FtpUtil {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
    private String username;
    private String password;
    private String host;
    private int port;
    
    private static FTPClient ftp;

    // 初始化
    public FtpUtil(String host, int port, String username, String password) {
    	this.username = username;
        this.password = password;
        this.host = host;
        this.port = port;
    }
    
    // 登录
    public boolean login() throws SocketException, IOException {
    	ftp = new FTPClient();
        ftp.connect(host, port);
        ftp.login(username, password);
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        	log.error("ftp登录失败");
			ftp.disconnect();
			return false;
        }
        else {
        	return true;
        }
    }
    
    // 登出
    public boolean logout() throws IOException {
    	return ftp.logout();
    }
    
    /**
     * 上传
     * @param path 服务器路径,如果文件夹不存在,则会一级级的创建
     * @param fileName 文件名
     * @param inputStream 输入流
     * @throws IOException 
     */
    public void uploadFile(String path, String fileName, InputStream inputStream) throws IOException {
    	
    	// 如果目录不存在就创建
    	if (!ftp.changeWorkingDirectory(path)) {
    		mkdir(path);
    	}
    	ftp.storeFile(path + "/" + fileName, inputStream);
    }
    
    /**
     * 上传
     * @param path 服务器路径,如果文件夹不存在,则会一级级的创建
     * @param fileName 文件名
     * @param str 文件内容
     * @throws IOException 
     */
    public void uploadFile(String path, String fileName, String str) throws IOException {
    	uploadFile(path, fileName, new ByteArrayInputStream(str.getBytes()));
    }
    
    /**
     * 下载
     * @param 文件路径
     * @throws IOException 
     */
    public byte[] downloadFile(String path) throws IOException {
    	return IOUtils.toByteArray(ftp.retrieveFileStream(path));
    }
    
    /**
     * 创建多级文件夹
     * @param path 路径,必须是绝对路径
     * 根据/分割,一级级的创建
     * @throws IOException 
     */
    public void mkdir(String path) throws IOException {
    	
    	if (!path.startsWith("/")) {
    		throw new RuntimeException("创建文件夹必须是以/开头的绝对路径");
    	}
    	
    	String tempPath = "";
    	String pathArr[] = path.split("/");
    	
    	for (String s : pathArr) {
    		
    		// 开头/和结尾/会分割出空字符串
    		if (!"".equals(s)) {
    			tempPath += "/" + s;
    			if (!ftp.changeWorkingDirectory(tempPath)) {
    				log.info("创建文件夹:" + tempPath);
    				ftp.mkd(tempPath);
    			}
    		}
    	}
    }
    
    public static void main(String[] args) throws SocketException, IOException {
    	
    	// 初始化
//		FtpUtil ftpUtil = new FtpUtil("127.0.0.1", 21, "ftp_user", "xxxxx");
		
		// 登录
//		System.out.println(ftpUtil.login());
		
		// 上传文件
//		ftpUtil.uploadFile("/home/ftp_user/ems_api/flow/a/b/c/d", "test.txt", new FileInputStream(new File("c:\\yingwenok.txt")));
		
		// 上传文件
//		ftpUtil.uploadFile("/home/ftp_user/ems_api/flow/a/b/c/d", "test.txt", "阿AV爱吃撒\r\nsadsd");
		
		// 下载文件
//		System.out.println(new String(ftpUtil.downloadFile("/home/ftp_user/ems_api/flow/a/b/c/d/test.txt")));
		
		//登出
//		System.out.println(ftpUtil.logout());
		
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值