解决java ftp 下载中文文件名乱码,0字节问题,

该博客介绍了如何使用Java处理FTP下载过程中遇到的中文文件名乱码和0字节问题。通过设置字符集为GBK,限制缓冲区大小,进入本地被动模式,并正确设置文件编码,实现了正常下载包含中文名的文件。同时,提供了下载单个文件和整个目录的方法。
摘要由CSDN通过智能技术生成

解决java ftp 下载中文文件名乱码,0字节问题,

public class FTPUtil {

	private String CHARSET = "GBK";
	// private String CHARSET = System.getProperty("file.encoding");

	/**
	 * 设置缓冲区大小4M
	 **/
	private int BUFFER_SIZE = 1024 * 1024 * 4;

	private FTPClient ftpClient = null;

	public void login(String address, int port, String username, String password) {
		ftpClient = new FTPClient();
		try {
			ftpClient.connect(address, port);
			ftpClient.login(username, password);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			// 限制缓冲区大小
			ftpClient.setBufferSize(BUFFER_SIZE);
			ftpClient.enterLocalPassiveMode();
			int reply = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpLogOut();
			}
//这里设置GBK
			ftpClient.setControlEncoding(CHARSET);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @退出关闭服务器链接
	 */
	public void ftpLogOut() {
		if (null != this.ftpClient && this.ftpClient.isConnected()) {
			try {
				boolean reuslt = this.ftpClient.logout();// 退出FTP服务器
				if (reuslt) {
					// logger.info("成功退出服务器");
				}
			} catch (IOException e) {
				e.printStackTrace();
				// logger.warn("退出FTP服务器异常!" + e.getMessage());
			} finally {
				try {
					this.ftpClient.disconnect();// 关闭FTP服务器的连接
				} catch (IOException e) {
					e.printStackTrace();
					// logger.warn("关闭FTP服务器的连接异常!");
				}
			}
		}
	}

	public boolean downloadFile(String remoteFileName, String localDires, String remoteDownLoadPath) {
		String strFilePath = localDires + remoteFileName;
		BufferedOutputStream outStream = null;
		boolean success = false;
		// File file = new File(strFilePath);

		try {
			// if (!file.exists()) {

			this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
			outStream = new BufferedOutputStream(new FileOutputStream(strFilePath));
		//注意这里gbk和iso-8859-1	
success = this.ftpClient.retrieveFile(new String(remoteFileName.getBytes(CHARSET), "iso-8859-1"), outStream);

			if (success == true) {
				return success;
			}
			// }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (null != outStream) {
				try {
					outStream.flush();
					outStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return success;
	}

	/***
	 * @下载文件夹
	 */
	public boolean downLoadDirectory(String localDirectoryPath, String remoteDirectory) {
		try {
			String fileName = new File(remoteDirectory).getName();
			localDirectoryPath = localDirectoryPath + fileName + "//";
			// new File(localDirectoryPath).mkdirs();
			File file = new File(localDirectoryPath);
			if (!file.exists()) {
				file.mkdir();
			}

			FTPFile[] allFile = this.ftpClient.listFiles(remoteDirectory);
			for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
				if (!allFile[currentFile].isDirectory()) {
					downloadFile(allFile[currentFile].getName(), localDirectoryPath, remoteDirectory);

				}
			}
			for (int currentFile = 0; currentFile < allFile.length; currentFile++) {
				if (allFile[currentFile].isDirectory()) {
					String strremoteDirectoryPath = remoteDirectory + "/" + allFile[currentFile].getName();
					downLoadDirectory(localDirectoryPath, strremoteDirectoryPath);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			// logger.info("下载文件夹失败");
			return false;
		}
		return true;
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值