java编写ftp下载工具
需要用到java写一个ftp的工具,因为只有一点点java基础,但是由于好几年不用,几乎算是不会了,只好一点点来搞,还好能捡起来。
不过因为是在Linux下使用javac编译,不是在WIN下使用IDE来做这些事情,所以在运行和编译上又费了一些时间,不过正是因为这样对JAVA的一些编译、运行的知识又了解了一些。
对于ftp下载工具,代码如下:
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.net.SocketException;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPReply;
publicclassFtpClient{
privateString host;
privateint port;
privateString username;
privateString password;
privateboolean binaryTransfer=true;
privateboolean passiveMode =true;
privateString encoding ="UTF-8";
privateint clientTimeout =3000;
privatebooleanflag=true;
privateFTPClientftpClient=null;
publicStringgetHost(){
returnhost;
}
publicvoidsetHost(Stringhost){
this.host=host;
}
publicintgetPort(){
returnport;
}
publicvoidsetPort(intport){
this.port=port;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicbooleanisBinaryTransfer(){
returnbinaryTransfer;
}
publicvoidsetBinaryTransfer(booleanbinaryTransfer){
this.binaryTransfer=binaryTransfer;
}
publicbooleanisPassiveMode(){
returnpassiveMode;
}
publicvoidsetPassiveMode(booleanpassiveMode){
this.passiveMode=passiveMode;
}
publicStringgetEncoding(){
returnencoding;
}
publicvoidsetEncoding(Stringencoding){
this.encoding=encoding;
}
publicintgetClientTimeout(){
returnclientTimeout;
}
publicvoidsetClientTimeout(intclientTimeout){
this.clientTimeout=clientTimeout;
}
publicFtpClient(StringHost){
this.username="anonymous";
this.encoding="utf-8";
this.binaryTransfer=true;
this.binaryTransfer=true;
this.port=21;
this.host=Host;
try{
this.ftpClient=getFTPClient();
}catch(Exceptione){
System.out.println("CreateFTPClienterror!");
}
}
privateFTPClientgetFTPClient()throwsIOException{
FTPClientftpClient=newFTPClient();
ftpClient.setControlEncoding(encoding);
connect(ftpClient);
if(passiveMode){
ftpClient.enterLocalPassiveMode();
}
setFileType(ftpClient);
try{
ftpClient.setSoTimeout(clientTimeout);
}catch(SocketExceptione){
thrownewIOException("Settimeouterror.",e);
}
returnftpClient;
}
privatevoidsetFileType(FTPClientftpClient)throwsIOException{
try{
if(binaryTransfer){
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
}else{
ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);
}
}catch(IOExceptione){
thrownewIOException("Couldnottosetfiletype.",e);
}
}
publicbooleanconnect(FTPClientftpClient)throwsIOException{
try{
ftpClient.connect(host,port);
intreply=ftpClient.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
if(ftpClient.login(username,password)){
setFileType(ftpClient);
returntrue;
}
}else{
this.ftpClient.disconnect();
thrownewIOException("FTPserverrefusedconnection.");
}
}catch(IOExceptione){
if(this.ftpClient.isConnected()){
try{
this.ftpClient.disconnect();
}catch(IOExceptione1){
thrownewIOException("Couldnotdisconnectfromserver.",e);
}
}
thrownewIOException("Couldnotconnecttoserver.",e);
}
returnfalse;
}
privatevoiddisconnect()throwsIOException{
try{
this.ftpClient.logout();
}catch(IOExceptione){
System.out.println("logoutmaytimeout!");
}finally{
if(this.ftpClient.isConnected()){
this.ftpClient.disconnect();
}
}
}
publicInputStreamgetStream(StringserverFile)throwsIOException{
InputStreaminStream=null;
try{
inStream=this.ftpClient.retrieveFileStream(serverFile);
System.out.println("inStreamgetover!");
returninStream;
}catch(IOExceptione){
System.out.println("getstreamexception");
returnnull;
}
}
publicbooleanwriteStream(InputStreaminput,StringlocalFile)throwsIOException{
FileOutputStreamfout=newFileOutputStream(localFile);
intch=0;
if(input==null){
System.out.println("inputisnull");
returnfalse;
}
try{
ch=input.read();
while(ch!=-1){
fout.write(ch);
ch=input.read();
}
System.out.println("writeover!");
returnflag;
}catch(IOExceptione){
thrownewIOException("Couldn'tgetfilefromserver.",e);
}
}
publicbooleanisExist(StringremoteFilePath)throwsIOException{
try{
Filefile=newFile(remoteFilePath);
StringremotePath=remoteFilePath.substring(0,(remoteFilePath.indexOf(file.getName())-1));
String[]listNames=this.ftpClient.listNames(remotePath);
System.out.println(remoteFilePath);
for(inti=0;i<listNames.length;i++){
System.out.println(listNames[i]);
if(remoteFilePath.equals(listNames[i])){
flag=true;
System.out.println("file:"+file.getName()+"existed");
break;
}else{
flag=false;
}
}
}catch(IOExceptione){
thrownewIOException("FILEEXCEPTION",e);
}
returnflag;
}
//mainfortesting
publicstaticvoidmain(String[]args)throwsIOException{
Stringhostname="cp01-testing-ps7130.cp01.baidu.com";
StringserverFile="/home/work/check_disk.sh";
StringlocalFile="/home/work/workspace/project/dhc2-0/dhc/base/ftp/task_get";
FtpClientftp=newFtpClient(hostname);
System.out.println(ftp.isExist(serverFile));
ftp.writeStream(ftp.getStream(serverFile),localFile);
ftp.disconnect();
}
}
这个工具是为了配合另外一个Hadoop工具做集群上传用的,所以里面的把input和output流分开了,也是为了方便另外一个工具使用。
补充一点,如何在linux配置运行:
如果这样的代码需要在linux下环境运行,首先要配置好响应的包,例如
importorg.apache.commons.net.ftp.FTPClient;
这个包在apache的网站上直接下载就行,解压后找到对应的jar包,在编译的时候进行引用:
exportFTPPATH="${路径}/xxx.jar"
javac-classpath$CLASSPATH:$FTPPATHFtpClient.java
同样,在运行的时候也要指定classpath:
java-classpath$CLASSPATH:$FTPPATHFtpClient
建议不要把$FTPPATH包含在CLASSPATH中,用什么包就引用什么环境变量就行了,没必要一股脑都添加进去,就像我们没必要import所有的包一样。
以上所述就是本文的全部内容了,希望能够对大家学习java有所帮助。
请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特