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

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

发布时间:2020-05-30 16:28:50 所属栏目:Java 来源:互联网
导读:ftp 实现文件的上传下载以及列出文件列表Java代码

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

publicclassFtpUtil{
	privateLoglog=LogFactory.getLog(getClass());
	
	privateStringuserName;
	privateStringpassword;
	privateStringip;
	privateintport;
	
	privateFTPClientftpClient=null;
	privateFTPSClientftps=null;
	
	//构造方法初始化类
	publicFtpUtil(StringuserName,Stringpassword,Stringip,intport){
		this.userName=userName;
		this.password=password;
		this.ip=ip;
		this.port=port;
	}
//连接ftp
	publicbooleanconnectServer()throwsException{
		booleanflag=true;
		if(ftpClient==null){
			ftpClient=newFTPClient();
			ftpClient.connect(ip,port);
			
			log.info("Connectedto"+ip);
			log.info(ftpClient.getReplyString());
			
			intreply=ftpClient.getReplyCode();
			if(!FTPReply.isPositiveCompletion(reply)){
				ftpClient.disconnect();
				log.warn("FTPserverrefusedconnection.");
				returnfalse;
			}
			
			booleanbok=ftpClient.login(userName,password);
			if(!bok){
				try{
					ftpClient.disconnect();
					ftpClient=null;
				}catch(Exceptione){}
				thrownewException("cannotloginftpserver");
			}
			
			ftpClient.setBufferSize(1024);
			ftpClient.setControlEncoding("GBK");
			ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
			ftpClient.setDataTimeout(120000);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setUseEPSVwithIPv4(false);
		}
		returnflag;
	}
//列出所有文件内容
	publicList<String>listRemoteAllFiles(Stringpath)throwsException{	
		ftpClient.enterLocalPassiveMode();
		FTPFile[]files=ftpClient.listFiles(path,newFTPFileFilter(){
			@Override
			publicbooleanaccept(FTPFilefile){
				if(file.isFile())returntrue;
				returnfalse;
			}});
		
		List<String>list=newArrayList();
		for(FTPFilefile:files){
			list.add(file.getName());
		}
		returnlist;
	}

	publicvoidcloseConnect(){
		try{
			if(ftpClient!=null){
				ftpClient.logout();
				ftpClient.disconnect();
			}
		}catch(Exceptione){
		}

	}
//下载文件
	publicbooleandownloadFile(StringremotePath,StringfileName,StringlocalPath)throwsException{
		
		FileOutputStreamfos=null;
		try{
			FilelocalFile=newFile(localPath,fileName);
			fos=newFileOutputStream(localFile);
			
			ftpClient.enterLocalPassiveMode();
			ftpClient.changeWorkingDirectory(remotePath);
			booleanbok=ftpClient.retrieveFile(fileName,fos);
			
			fos.close();
			fos=null;
			
			returnbok;
		}catch(Exceptione){
			throwe;
		}
		finally{
			if(fos!=null){
				try{
					fos.close();
					fos=null;
				}catch(Exceptione2){}
			}
		}
		
	}
//上传文件
	publicbooleanuploadFile(StringremotePath,Stringfilename,StringlocalFilePath)throwsException{
		FileInputStreamfis=null;
		try{
			fis=newFileInputStream(newFile(localFilePath));
			
			ftpClient.enterLocalPassiveMode();
			ftpClient.changeWorkingDirectory(remotePath);
			booleanbok=ftpClient.storeFile(filename,fis);
			
			fis.close();
			fis=null;
			
			returnbok;
		}catch(Exceptione){
			throwe;
		}
		finally{
			if(fis!=null){
				try{
					fis.close();
					fis=null;
				}catch(Exceptione2){}
			}
		}

	}
	//删除文件
	publicbooleanremoveFile(StringremotePath,Stringfilename)throwsException{
		ftpClient.changeWorkingDirectory(remotePath);
		booleanbok=ftpClient.deleteFile(filename);
		returnbok;
	}

}

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

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

    推荐文章
      热点阅读