java ftp 类_java ftp操作类

1 importjava.io.BufferedReader;2 importjava.io.File;3 importjava.io.FileInputStream;4 importjava.io.FileOutputStream;5 importjava.io.IOException;6 importjava.io.InputStreamReader;7 importjava.io.OutputStream;8 importjava.util.ArrayList;9 importjava.util.List;10 importjava.util.StringTokenizer;11 12 importsun.net.TelnetInputStream;13 importsun.net.TelnetOutputStream;14 importsun.net.ftp.FtpClient;15 19 publicclassFtpUtil {20 21 privateString ip="";22 privateString userName="";23 privateString password="";24 privateintport=-1;25 privateString path="";26 FtpClient ftpClient=null;27 OutputStream os=null;28 FileInputStream is=null;29 30 publicFtpUtil(String serverIP, String userName, String password) {31 this.ip=serverIP;32 this.userName=userName;33 this.password=password;34 }35 36 publicFtpUtil(String serverIP,intport, String userName, String password) {37 this.ip=serverIP;38 this.userName=userName;39 this.password=password;40 this.port=port;41 }42 43 publicbooleanconnectServer() {44 ftpClient=newFtpClient();45 try{46 if(this.port!=-1) {47 ftpClient.openServer(this.ip,this.port);48 }else{49 ftpClient.openServer(this.ip);50 }51 ftpClient.login(this.userName,this.password);52 if(this.path.length()!=0) {53 ftpClient.cd(this.path);54 }55 ftpClient.binary();56 returntrue;57 }catch(IOException e) {58 e.printStackTrace();59 returnfalse;60 }61 }62 63 publicbooleancloseServer() {64 try{65 if(is!=null) {66 is.close();67 }68 if(os!=null) {69 os.close();70 }71 if(ftpClient!=null) {72 ftpClient.closeServer();73 }74 returntrue;75 }catch(IOException e) {76 e.printStackTrace();77 returnfalse;78 }79 }80 81 /**82 * judge if dir exist on ftp83 *84 *@paramdir85 *@return86 */87 publicbooleanisDirExist(String dir) {88 String pwd="";89 try{90 pwd=ftpClient.pwd();91 ftpClient.cd(dir);92 ftpClient.cd(pwd);93 }catch(Exception e) {94 returnfalse;95 }96 returntrue;97 }98 99 /**100 * create directory on ftp101 *102 *@paramdir103 *@return104 */105 privatebooleancreateDir(String dir) {106 try{107 ftpClient.ascii();108 StringTokenizer s=newStringTokenizer(dir,"/");//sign109 s.countTokens();110 String pathName=ftpClient.pwd();111 while(s.hasMoreElements()) {112 pathName=pathName+"/"+(String) s.nextElement();113 try{114 ftpClient.sendServer("MKD"+pathName+"\r\n");115 }catch(Exception e) {116 e=null;117 returnfalse;118 }119 ftpClient.readServerResponse();120 }121 ftpClient.binary();122 returntrue;123 }catch(IOException e1) {124 e1.printStackTrace();125 returnfalse;126 }127 }128 129 /**130 * upload file or folder (auto generate ftp filename)131 *132 *@paramname133 *@return134 */135 publicbooleanupload(String name) {136 String newname="";137 if(name.indexOf("/")>-1) {138 newname=name.substring(name.lastIndexOf("/")+1);139 }else{140 newname=name;141 }142 returnupload(name, newname);143 }144 145 /**146 * upload file or folder147 *148 *@paramname149 *@paramnewName150 *@return151 */152 publicbooleanupload(String name, String newName) {153 try{154 String savefilename=newString(name.getBytes("ISO-8859-1"),"GBK");155 File file_in=newFile(savefilename);156 if(!file_in.exists()) {157 thrownewException("file or folder"+file_in.getName()158 +"not exist!");159 }160 if(file_in.isDirectory()) {161 upload(file_in.getPath(), newName, ftpClient.pwd());162 }else{163 uploadFile(file_in.getPath(), newName);164 }165 166 if(is!=null) {167 is.close();168 }169 if(os!=null) {170 os.close();171 }172 returntrue;173 }catch(Exception e) {174 e.printStackTrace();175 System.err.println("Exception e in Ftp upload():"+e.toString());176 returnfalse;177 }finally{178 try{179 if(is!=null) {180 is.close();181 }182 if(os!=null) {183 os.close();184 }185 }catch(IOException e) {186 e.printStackTrace();187 }188 }189 }190 191 /**192 * upload file/folder to ftp193 *194 *@paramname195 *@paramnewName196 *@parampath197 *@throwsException198 */199 privatevoidupload(String name, String newName, String path)200 throwsException {201 String savefilename=newString(name.getBytes("ISO-8859-1"),"GBK");202 File file_in=newFile(savefilename);203 if(!file_in.exists()) {204 thrownewException("file or folder"+file_in.getName()205 +"!not exist");206 }207 if(file_in.isDirectory()) {208 if(!isDirExist(newName)) {209 createDir(newName);210 }211 ftpClient.cd(newName);212 File sourceFile[]=file_in.listFiles();213 for(inti=0; i-1) {346 fileName=fullName.substring(fullName.lastIndexOf("/")+1);347 }else{348 fileName=fullName;349 }350 returnfileName;351 }352 353 /**354 * get file list in the directory355 *356 *@parampath357 *@return358 */359 publicList getFileList(String path) {360 List list=newArrayList();361 BufferedReader br;362 try{363 br=newBufferedReader(newInputStreamReader(ftpClient364 .nameList(this.path+path)));365 String filename="";366 while((filename=br.readLine())!=null) {367 list.add(filename);368 }369 }catch(IOException e) {370 e.printStackTrace();371 }372 returnlist;373 }374 375 /**376 * download ftp directory to local377 *378 *@paramftpIp379 *@paramftpPort380 *@paramftpUser381 *@paramftpPassword382 *@paramftpDir383 *@paramlocalDir384 */385 publicstaticvoidgetDirFromFtp(String ftpIp,intftpPort, String ftpUser,386 String ftpPassword, String ftpDir, String localDir) {387 FtpUtil ftp=newFtpUtil(ftpIp, ftpPort, ftpUser, ftpPassword);388 ftp.connectServer();389 ftp.downloadDir(ftpDir, localDir);390 ftp.closeServer();391 }392 393 publicstaticvoidmain(String[] args) {394 FtpUtil ftp=newFtpUtil("10.2.23.122",2121,"user","password");395 ftp.connectServer();396 ftp.downloadDir("test","e:/temp");397 ftp.closeServer();398 }399 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值