FTPClient 处理多个文件时注意添加completePendingCommand

<span style="font-family:Arial, Helvetica, sans-serif;">//楼主之前做一个项目对接,要求用到操作ftp文件等功能,主要遇到的问题是当要遍历文件夹里的文件时或者下载所有文件时,如果没有使用completePendingCommand()这方//法,则只能处理一个文件,在处理第二个文件的时候(即第二次调用retrieveFileStream()方法的时候)返回null。</span>
<span style="font-family:Arial, Helvetica, sans-serif;">//所以处理第二个文件前,必须使用completePendingCommand()方法</span>


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


/**
 * 先链接,再进行操作
 
 */
public class FTPUtil {


	private FTPClient ftp;
	
	//链接ftp
	public boolean connect(String path,String addr,int port,String username,String password){
		ftp = new FTPClient();
		try {
//			ftp.connect(addr, port);
			ftp.connect(addr);
			ftp.login(username, password);
			if(!FTPReply.isPositiveCompletion(ftp.getReplyCode())){
				ftp.disconnect();
				return false;
			}
			ftp.changeWorkingDirectory(path);
			return true;
		} catch (SocketException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//上传文件
	public boolean upLoadFile(File file){
		try {
			FileInputStream input = new FileInputStream(file);
			ftp.storeFile(file.getName(), input);
			input.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//断开连接
	public boolean disconnect(){
		try {
			return ftp.logout();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//获取对应文件夹里的所有文件
	public FTPFile[] listFile(){
		try {
			return ftp.listFiles();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//返回输入流
	public InputStream returnFileStream(String fileName){
		try {
			return ftp.retrieveFileStream(fileName);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//设置处理多个文件
	public boolean completePendingCommand(){
		try {
			return ftp.completePendingCommand();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//删除文件
	public boolean deleteFile(String fileName){
		try {
			return ftp.deleteFile(fileName);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	

}

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FTPClient是Java中用于处理FTP连接和文件传输的库,但是它本身并不支持文件合并的功能。如果要合并文件,需要先将文件下载到本地,再使用Java中的IO操作将多个文件合并成一个文件。 以下是一个简单的实现步骤: 1. 创建FTP连接并登录。 ``` FTPClient ftpClient = new FTPClient(); ftpClient.connect(server, port); ftpClient.login(username, password); ``` 2. 切换到需要下载文件的目录,并下载需要合并的文件。 ``` ftpClient.changeWorkingDirectory(remoteDir); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); File localFile = new File(localDir, remoteFileName); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); ftpClient.retrieveFile(remoteFileName, outputStream); outputStream.close(); ``` 3. 重复步骤2,下载所有需要合并的文件到本地。 4. 使用Java中的IO操作将多个文件合并成一个文件。 ``` File mergedFile = new File(localDir, mergedFileName); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(mergedFile)); for (int i = 0; i < fileNames.size(); i++) { File file = new File(localDir, fileNames.get(i)); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } inputStream.close(); } outputStream.close(); ``` 5. 关闭FTP连接。 ``` ftpClient.logout(); ftpClient.disconnect(); ``` 需要注意的是,如果需要合并的文件比较大,这种方式可能会占用大量的内存,可以考虑使用Java中的NIO或者Apache Commons IO库来处理文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值