用apache的commons-net包中的ftp实现的ftp上传下载

狄峰
2023-12-01
/**
 * File Created at 2015年8月4日
 *
 * Copyright 2015 star.com Limited.
 * All rights reserved.
 */
package com.star.daffodil.common.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
 
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
 
 
/**
 * <p>
 * FTP上传下载工具类
 * </p>
 * @author qunxing.du
 *
 */
public class FTPUtil {
	
	private static String HOSTNAME = "127.0.0.1";
	private static int PORT = 21;
	private static String USERNAME = "admin";
	private static String PASSWORD = "admin";
	
	private static FTPClient ftp;
	
	private FTPUtil(){}
	
	private static FTPUtil ftpUtil;
	public static FTPUtil getInstance(){
		if(ftp == null){
			ftp = new FTPClient();
		}
		if(ftpUtil == null){
			ftpUtil = new FTPUtil();
		}
		return ftpUtil;
	}
	
	/**
	 * 连接ftp服务器
	 */
	private void connect(){
		FTPClientConfig ftpClientConfig = new FTPClientConfig(FTPClientConfig.SYST_NT);
		ftp.configure(ftpClientConfig);
		try {
			ftp.connect(HOSTNAME, PORT);
			ftp.login(USERNAME, PASSWORD);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 退出关闭连接
	 */
	private void close(){
		if(ftp.isConnected()){
			try {
				ftp.logout();
			} catch (IOException e) {
				e.printStackTrace();
			} finally{
				try {
					ftp.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public boolean upload(String dest, InputStream is){
		boolean flag = false;
		connect();
		int reply = ftp.getReplyCode();
		if(!FTPReply.isPositiveCompletion(reply)){
			close();
			return flag;
		}
		try {
			String destFile = dest.replace("\\\\", "/");
			String destDir = destFile.substring(0, destFile.lastIndexOf("/"));
			
			FTPFile[] ftpFiles = ftp.listDirectories("\\");
			boolean isExist = false;
			for (FTPFile ftpFile : ftpFiles) {
				if(destDir.equals(ftpFile.toString())){
					isExist = true;
					break;
				}
			}
			
			if(!isExist){
				ftp.makeDirectory(destDir);
			}
			
			ftp.setFileType(FTP.BINARY_FILE_TYPE);
			flag = ftp.storeFile(dest, is);
			if(flag){
				System.out.println("上传成功");
			}else{
				System.out.println("上传失败");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is != null ){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			close();
		}
		return flag;
	}
	
	public InputStream download(String file){
		InputStream is = null;
		connect();
		int reply = ftp.getReplyCode();
		if (!FTPReply.isPositiveCompletion(reply)) {
			close();
			return null;
		}
		try {
			is = ftp.retrieveFileStream(file);
			if(ftp.completePendingCommand()){
				System.out.println("下载流读取完成");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			close();
		}
		
		return is;
	}
	public static void main(String[] args) {
		InputStream is = getInstance().download("/upload11/d21/gif/1439029019947.gif");
		FileOutputStream fos = null;
		File file = new File("D:/a2.gif");
		try {
			Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		InputStream uis = null;
		try {
			uis = new FileInputStream("D:/a2.gif");
			getInstance().upload("upload11/d21/gif/" + GlobalUtil.getUniqueNumber() + ".gif", uis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally{
			if(uis != null){
				try {
					uis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
}
 

 

 类似资料: