加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

ftp 实现文件的上传下载以及列出文件列表Java代码

发布时间:2020-12-15 03:13:08 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public?class?FtpUtil?{private??Log?log?=?LogFactory.getLog(getClass())?;private?String?userName;?private?String?password;private?String?ip;?

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

public?class?FtpUtil?{
	private??Log?log?=?LogFactory.getLog(getClass())?;
	
	private?String?userName;?
	private?String?password;
	private?String?ip;?
	private?int?port;?
	
	private?FTPClient?ftpClient?=?null;?
	private?FTPSClient?ftps?=?null?;
	
	//构造方法初始化类
	public?FtpUtil(String?userName,?String?password,?String?ip,?int?port)?{
		this.userName?=?userName;
		this.password?=?password;
		this.ip?=?ip;
		this.port?=?port;
	}
????????//连接ftp
	public?boolean?connectServer()?throws?Exception{
		boolean?flag?=?true;
		if?(ftpClient?==?null)?{?
			ftpClient?=?new?FTPClient();
			ftpClient.connect(ip,port);
			
			log.info("Connected?to?"?+?ip);
			log.info(ftpClient.getReplyString());
			
			int?reply?=?ftpClient.getReplyCode();?
			if?(!FTPReply.isPositiveCompletion(reply))?{
				ftpClient.disconnect();
				log.warn("FTP?server?refused?connection.");
				return?false?;
			}
			
			boolean?bok?=?ftpClient.login(userName,?password);
			if?(!bok)??{
				try?{
					ftpClient.disconnect()?;
					ftpClient?=?null?;
				}?catch?(Exception?e)?{?}
				throw?new?Exception("can?not?login?ftp?server")?;
			}
			
			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("GBK");?
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);?
			ftpClient.setDataTimeout(120000);?
			ftpClient.enterLocalPassiveMode();
			ftpClient.setUseEPSVwithIPv4(false);
		}?
		return?flag;
	}
????????//列出所有文件内容
	public?List<String>?listRemoteAllFiles(String?path)?throws?Exception?{	
		ftpClient.enterLocalPassiveMode();?
		FTPFile[]?files?=?ftpClient.listFiles(path,?new?FTPFileFilter()?{?
			@Override
			public?boolean?accept(FTPFile?file)?{?
				if?(file.isFile())?return?true?;
				return?false?;
			}})?;
		
		List<String>?list?=?new?ArrayList()?;
		for?(FTPFile?file?:?files)?{
			list.add(file.getName())?;
		}
		return?list?;
	}

	public?void?closeConnect()?{?
		try?{
			if?(ftpClient?!=?null)?{
				ftpClient.logout();
				ftpClient.disconnect();
			}
		}?catch?(Exception?e)?{
		}

	}
????????//下载文件
	public?boolean?downloadFile(String?remotePath,?String?fileName,?String?localPath)?throws?Exception?{
		
		FileOutputStream?fos?=?null?;?
		try?{
			File?localFile?=?new?File(localPath,?fileName);
			fos?=?new?FileOutputStream(localFile);
			
			ftpClient.enterLocalPassiveMode();?
			ftpClient.changeWorkingDirectory(remotePath)?;
			boolean?bok?=?ftpClient.retrieveFile(fileName,?fos);
			
			fos.close()?;
			fos?=?null?;
			
			return?bok?;
		}?catch?(Exception?e)?{
			throw?e?;
		}
		finally?{
			if?(fos!=null)?{
				try?{
					fos.close()?;
					fos?=?null?;
				}?catch?(Exception?e2)?{?}
			}
		}?
		
	}
????????//上传文件
	public?boolean?uploadFile(String?remotePath,?String?filename,?String?localFilePath)?throws?Exception?{
		FileInputStream?fis?=?null?;
		try?{
			fis?=?new?FileInputStream(new?File(localFilePath));
			
			ftpClient.enterLocalPassiveMode();?
			ftpClient.changeWorkingDirectory(remotePath);
			boolean?bok?=?ftpClient.storeFile(filename,?fis);?
			
			fis.close();
			fis?=?null?;
			
			return?bok?;
		}?catch?(Exception?e)?{
			throw?e?;
		}
		finally?{
			if?(fis!=null)?{
				try?{
					fis.close()?;
					fis?=?null?;
				}?catch?(Exception?e2)?{?}
			}
		}

	}
	//删除文件
	public?boolean?removeFile(String?remotePath,?String?filename)?throws?Exception?{
		ftpClient.changeWorkingDirectory(remotePath);
		boolean?bok?=?ftpClient.deleteFile(filename)?;?
		return?bok?;
	}

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读