首页 > Java > java教程 > 正文

ftp上传实例代码

PHP中文网
发布: 2017-06-20 09:51:25
原创
1566人浏览过

/**
* ftp上传工具类
*
*/
public class ftputil {
static logger logger = logger.getlogger(ftputil.class.getname());
   private string ip;
   private string username;
   private string password;
   private int port = -1;
   private string path;
   private outputstream os = null;
   private fileinputstream is = null;
private ftpclient ftpclient;
public ftpclient getftpclient() { return ftpclient; }
public void setftpclient(ftpclient ftpclient) {
this.ftpclient = ftpclient;
}
public ftputil(){

}
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 password) {
 this.ip = serverip;
 this.username = username;
 this.password = password;
 this.port = port;
 this.ftpclient= new ftpclient();
}
/**
 *
 * @return 是否连接服务器
 */
public boolean ftplogin() {  
boolean islogin = false;  
ftpclientconfig ftpclientconfig = new ftpclientconfig();  
ftpclientconfig.setservertimezoneid(timezone.getdefault().getid());  
this.ftpclient.setcontrolencoding("gbk");  
this.ftpclient.configure(ftpclientconfig);  
try {    
if (this.port > 0) {    
this.ftpclient.connect(this.ip, this.port);    
}else {    
this.ftpclient.connect(this.ip);    
}  
int reply = this.ftpclient.getreplycode();    
if (!ftpreply.ispositivecompletion(reply)) {    
this.ftpclient.disconnect();
logger.info("登录ftp服务失败!");  
return islogin;    
}    
if(this.ftpclient.login(this.username, this.password)){
this.ftpclient.enterlocalpassivemode(); // 设置传输协议        
this.ftpclient.setfiletype(ftpclient.binary_file_type);
logger.info(this.username + "成功登陆ftp服务器");  
islogin = true;
}else{
logger.info(this.username + "登录ftp服务失败!");
}
}catch (exception e) {    
e.printstacktrace();  
logger.error( e.getmessage());  
}  
this.ftpclient.setbuffersize(1024 * 2);  
this.ftpclient.setdatatimeout(30 * 1000);  
return islogin;  
}
/**  
* @退出关闭服务器链接    
*/  
public void ftplogout() {  
if (null != this.ftpclient && this.ftpclient.isconnected()) {    
try {    
boolean reuslt = this.ftpclient.logout();// 退出ftp服务器      
if (reuslt) {  
logger.info("成功退出服务器");  
}    
}catch (ioexception e) {    
e.printstacktrace();  
logger.warn("退出ftp服务器异常!" + e.getmessage());    
}finally {    
try {      
this.ftpclient.disconnect();
}catch (ioexception e) {      
e.printstacktrace();
logger.error("关闭ftp服务器的连接异常!");    
}    
}  
}  
}
/**  
* 上传ftp文件    
* @param localfile           当地文件  
* @param romotuploadepath    上传服务器路径    
*/  
public boolean uploadfile(file localfile, string romotuploadepath) {  
bufferedinputstream instream = null;  
boolean success = false;  
try {    
this.ftpclient.changeworkingdirectory(romotuploadepath);
instream = new bufferedinputstream(new fileinputstream(localfile));  
success = this.ftpclient.storefile(localfile.getname(), instream);    
if (success == true) {  
system.out.println(localfile.getname() + "上传成功");///
return success;    
}  
}catch (filenotfoundexception e) {    
e.printstacktrace();  
}catch (ioexception e) {    
e.printstacktrace();  
}finally {    
if (instream != null) {    
try {      
instream.close();    
}catch (ioexception e) {      
e.printstacktrace();    
}    
}  
}  
return success;  
}  

/**上传文件夹    
* @param localdirectory       本地文件夹      
* @param remotedirectorypath  远程路径
*/  
public boolean uploaddirectory(string localdirectory,string remotedirectorypath) {  
file src = new file(localdirectory);  
system.out.println(src.getname());
try {    
remotedirectorypath = remotedirectorypath + src.getname() + "/";    
boolean makedirflag = ftpclient.makedirectory(remotedirectorypath);    
system.out.println("localdirectory : " + localdirectory);    
system.out.println("remotedirectorypath : " + remotedirectorypath);    
system.out.println("src.getname() : " + src.getname());    
system.out.println("remotedirectorypath : " + remotedirectorypath);    
system.out.println("makedirflag : " + makedirflag);  
// ftpclient.listdirectories();  
}catch (ioexception e) {    
e.printstacktrace();
}  
file[] allfile = src.listfiles();
if(allfile.length==0||allfile.length>0){
for (int currentfile = 0;currentfile if (!allfile[currentfile].isdirectory()) {    
string srcname = allfile[currentfile].getpath().tostring();    
uploadfile(new file(srcname), remotedirectorypath);    
}
}  
for (int currentfile = 0;currentfile if (allfile[currentfile].isdirectory()) {
uploaddirectory(allfile[currentfile].getpath().tostring(),  //递归      
remotedirectorypath);    
}  
}  
}
/*for (int currentfile = 0;currentfile if (!allfile[currentfile].isdirectory()) {    
string srcname = allfile[currentfile].getpath().tostring();    
uploadfile(new file(srcname), remotedirectorypath);    
}
}  
for (int currentfile = 0;currentfile if (allfile[currentfile].isdirectory()) {
uploaddirectory(allfile[currentfile].getpath().tostring(),  //递归      
remotedirectorypath);    
}  
}   */
return true;  
}  

以上就是ftp上传实例代码的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号