FTP文件上传(纯代码).docx

上传人:自*** 文档编号:126205317 上传时间:2020-03-23 格式:DOCX 页数:9 大小:34.93KB
返回 下载 相关 举报
FTP文件上传(纯代码).docx_第1页
第1页 / 共9页
FTP文件上传(纯代码).docx_第2页
第2页 / 共9页
FTP文件上传(纯代码).docx_第3页
第3页 / 共9页
FTP文件上传(纯代码).docx_第4页
第4页 / 共9页
FTP文件上传(纯代码).docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《FTP文件上传(纯代码).docx》由会员分享,可在线阅读,更多相关《FTP文件上传(纯代码).docx(9页珍藏版)》请在金锄头文库上搜索。

1、完整程序FTP上传package com.zhcw.update.msisdn;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import java.util.StringTokenizer;import .T

2、elnetInputStream;import .TelnetOutputStream;import .ftp.FtpClient;/* * Title: FTP上传功能类 * Description: FTP上传功能类 * Copyright: Copyright (c) 2012 * Company: 北京世纪国彩科技有限公司 * * author chenchuang * version 1.0 */public class FtpUtil private String ip=;private String username=;private String password=;priva

3、te int port=-1;private String path=;FtpClient ftpClient=null;OutputStream os=null;FileInputStream is=null;public FtpUtil(String serverIp ,String username,String password)this.ip=serverIp;this.username=username;this.password=password;public FtpUtil(String serverIp , int port,String username,String pa

4、ssword)this.ip=serverIp;this.port=port;this.username=username;this.password=password;public static void main(String args)FtpUtil ftp=new FtpUtil(172.16.10.142,admin,adminly);ftp.connectServer();boolean result=ftp.upload(C:/Documents and Settings/SJKC/桌面/java/cc.txt, cc.txt);/boolean result=ftp.downl

5、oadFile(C:/Documents and Settings/SJKC/桌面/java/cc.txt, cc.txt);System.out.println(result ? 上传成功! : 上传失败!);ftp.closeServer();/* * ftp上传 如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换 * param fileName 要上传的文件(或文件夹)名 * param newName 服务器端要生成的文件(或文件夹)名 * return */public boolean upload(String fileName,Stri

6、ng newName)tryString savefilename=new String(fileName.getBytes(GBK),GBK);File file_in = new File(savefilename);/ 打开本地待长传的文件if (!file_in.exists() throw new Exception(此文件或文件夹 + file_in.getName() + 有误或不存在!);if (file_in.isDirectory() upload(file_in.getPath(), newName, ftpClient.pwd(); else uploadFile(fi

7、le_in.getPath(), newName);if (is != null) is.close();if (os != null) os.close();return true; catch (Exception e) e.printStackTrace();System.err.println(Exception e in Ftp upload(): + e.toString();return false; finally try if (is != null) is.close();if (os != null) os.close(); catch (IOException e) e

8、.printStackTrace();/* * 真正用于上传的方法 * param fileName * param newName * param path */private void upload(String fileName, String newName, String path) throws ExceptionString savefilename=new String(fileName.getBytes(ISO-8859-1),GBK);File file_in=new File(savefilename);/打开本地待上传的文件if(!file_in.exists()thr

9、ow new Exception(此文件或文件夹+file_in.getName()+有误或不存在!);if(file_in.isDirectory()if(!isDirExist(newName)createDir(newName);ftpClient.cd(newName);File sourceFile=file_in.listFiles();for(int i=0;isourceFile.length;i+)if(!sourceFilei.exists()continue;if(sourceFilei.isDirectory()this.upload(sourceFilei.getPa

10、th(),sourceFilei.getName(),path+/+newName);elsethis.upload(sourceFilei.getPath(),sourceFilei.getName();elseuploadFile(file_in.getPath(),newName);ftpClient.cd(path);/* * 在当前目录下创建文件夹 * param dir * return */private boolean createDir(String dir)tryftpClient.ascii();/转换到字符模式下StringTokenizer s=new StringT

11、okenizer(dir,/);/sign(记号,符号)s.countTokens();/* * countTokenspublic int countTokens()计算在生成异常之前可以调用此 tokenizer 的 nextToken 方法的次数。当前位置没有提前。 返回:使用当前分隔符集的字符串中剩余的标记数。以上是API中的说明,我写了一个小程序:import java.util.*;public class Tokenizer public static void main(String args) StringTokenizer a = new StringTokenizer(a

12、 b c); System.out.println(a.countTokens(); 程序执行的结果是3,因为a b c采用默认的分隔符分隔,一共有a,b,c三个token,所以countTokens()的结果是3 */String pathName=ftpClient.pwd();while(s.hasMoreElements()pathName=pathName+/+(String)s.nextElement();tryftpClient.sendServer(MKD +pathName+rn);catch(Exception e)e=null;return false;ftpClient

13、.readServerResponse();ftpClient.binary();return true;catch(IOException ee)ee.printStackTrace();return false;/* * 连接ftp服务器 * return */public boolean connectServer()ftpClient=new FtpClient();tryif(this.port!=-1)ftpClient.openServer(this.ip,this.port);elseftpClient.openServer(this.ip);ftpClient.login(this.username, this.password);if

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号