java-commons包操作ftp

工具类方式

详情:https://www.cnblogs.com/BambooLamp/p/12703187.html

说明

针对linux版ftp,本地用户,被动模式,代码之前,先保证用ftp客户端实现文件的上传、下载、删除。

建立连接

点击查看代码
	/**
	 * 获取连接
	 */
	public FTPClient getConnection() {
		FTPClient ftpClient = new FTPClient();
		try {
			int reply;
			//ftp域名
			String host = this.getFtpServerIp();
			//ftp端口号
			Integer port = this.getFtpPort();
			//连接ftp
			ftpClient.connect(host, Integer.valueOf(port));
			//账号
			String username = this.getFtpUser();
			//密码
			String password = this.getFtpPw();
			//登录ftp
			ftpClient.login(username, password);
			//设置编码
			ftpClient.setControlEncoding("utf-8");
			//将客户端设置为被动模式
			ftpClient.enterLocalPassiveMode();
			//申请方式
			reply = ftpClient.getReplyCode();
			//设置文件类型为二进制
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
			}
//            ftpClient.setControlEncoding(CommonUtils.localCharset);
		} catch (Exception e) {
			logger.error("获取ftp连接失败!!");
			e.printStackTrace();
		} finally {
			return ftpClient;
		}
	}

上传

点击查看代码
	/**
	 * 上传文件至ftp
	 * @param ftpPath 分类文件目录(例子:img/20220619	)
	 * @param inputStream 文件流
	 * @param fileName 文件名
	 * @return
	 * @throws IOException
	 */
	 public boolean uploadFile2Ftp(String ftpPath, InputStream inputStream,String fileName) throws IOException{
		FTPClient ftpClient = new FTPClient();
		InputStream fis = null;
		try {
			ftpClient.connect(ftpServerIp, ftpPort);
			ftpClient.login(ftpUser, ftpPw);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

			int reply = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				return false;
			}
			//判断FPT目标文件夹时候存在不存在则创建

			String[] dirs = ftpPath.split("/");
			for (String dir : dirs) {
				if(!ftpClient.changeWorkingDirectory(dir)) {
					boolean isExist = ftpClient.makeDirectory(dir);
					if (isExist) {
						//跳转目标目录
						ftpClient.changeWorkingDirectory(dir);
					}
				}
			}
			ftpClient.storeFile(fileName, inputStream);

		} catch (Exception e) {
			logger.error("上传文件到ftp失败", e);
			return false;
		} finally {
			if (fis != null) fis.close();
			ftpClient.disconnect();
		}
		return true;
	}

下载

点击查看代码
	/**
	 * 下载图片
	 *
	 * @param fileName 文件名------如: one.jpg  user.txt等等
	 * @return
	 * @throws IOException
	 */
	public InputStream downloadFile(String fileName) {
		InputStream in = null;
		String preFtpUrl = "ftp://" + this.getFtpServerIp() + ":" + this.getFtpPort() + "/";
		try {
			//建立连接
			FTPClient ftpClient = getConnection();
			ftpClient.enterLocalPassiveMode();
			// 设置传输二进制文件
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
			int reply = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
			}
			ftpClient.changeWorkingDirectory(preFtpUrl);
			// ftp获取文件
			in = ftpClient.retrieveFileStream(new String(fileName.getBytes(), "ISO-8859-1"));
		} catch (Exception e) {
			logger.error(new Date().toString()+"-------------ftp下载文件失败:-------------------");
			e.printStackTrace();
		} finally {
			return in;
		}
	}

删除

点击查看代码
	/**
	 * 删除文件,需和上传函数配套
	 * @param fileName 文件名(文件上传成功后ftp返回的路径,例子:/user/20220618/424.png)
	 * @return
	 */
	public boolean deleteFile(String fileName) {
		try {
			//建立连接
			FTPClient ftpClient = getConnection();
			// 被动模式
			ftpClient.enterLocalPassiveMode();
			// 跳转到该文件夹
			ftpClient.changeWorkingDirectory(new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
			ftpClient.deleteFile(new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
		} catch (Exception e) {
			logger.error(new Date().toString()+"-------------ftp删除文件失败:-------------------{}",fileName);
			e.printStackTrace();
			return false;
		}
		return true;
	}
posted @ 2022-06-19 16:31  jf666new  阅读(89)  评论(0)    收藏  举报