首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 冲浪宝典-> QQ专栏
java做的比较完善的FTP连接上传下载文件-JSP教程,Java技巧及代码
作者:网友供稿 点击:271
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
这是偶第一次写java程序实现公司要求的ftp数据同步,所有原代码如下,以供各位参考并提出宝贵意见!另外俺还有两个功能没有实现,有请高手指点一二:1、如何删除ftp服务器上已经下载过的文件?2、如何将上传的文件改名?上传时在文件名前加“temp_”,上传完毕后需要把文件名改回来。3、如何使用openpassivedataconnection()方法将ftp连接更改为主动连接,因为要从unix系统配置的ftp服务器下载文件的话,就需要主动连接。



/*
*******************************************************************************************************

filename: ftp.java
author: leetsing(elove)
create date: 2004-08-30
use: connect to ftp server,then upload and download file
modify date: 2004-09-05 add to upload file
2004-09-13 add to download file
copy right: magisky media technology co.,ltd.

*******************************************************************************************************
*/
//import cz.dhl.io.*;
//import cz.dhl.ftp.*;
import sun.net.ftp.*;
import sun.net.*;
import java.applet.*;
import java.io.*;
import java.io.ioexception;
import java.util.stringtokenizer;
import sun.net.ftp.ftpclient;
import java.util.arraylist;

public class ftp extends applet
{
ftpclient aftp;
dataoutputstream outputs ;
telnetinputstream ins;
telnetoutputstream outs;

int ch;
public string a;
string hostname="";
private string path = "/";

public static void main(string[] args)
{
string hostname = "192.168.0.56";
int port = 2121;
string uid = "lee";
string pwd = "lee";
string rwfiledir = "d:\\smsftp\\";//文件目录

//连接ftp服务器
ftp ft = new ftp();
ft.connect(rwfiledir,hostname,port,uid,pwd);

//下载文件
if (ft.aftp != null){
try {
ft.getnamelist(rwfiledir);
}catch(ioexception e)
{
system.out.println("下载文件出错:"+e);
}
}


//上传文件
if (ft.aftp != null){
string sdir = rwfiledir + "subunsubfromsp\\";
file fdir = new file(sdir);
string filename = "";

for(int i=0;i filename = sdir + (fdir.list())[i];

ft.uploadfile(rwfiledir,filename);
}
//system.out.println("成功上传的文件:");
//ft.showfilecontents("subunsubfromsp\\");
}

//删除subunsubfromsp目录下已经上传的文件文件
// ft.delefile(rwfiledir);

//断开服务器连接
ft.stop(rwfiledir);

}

public ftpclient connect(string rwfiledir,string hostname,int port,string uid,string pwd)
{
this.hostname = hostname;
system.out.println("正在连接"+hostname+",请等待.....");
try{
aftp = new ftpclient(hostname,port);
aftp.login(uid,pwd);
aftp.binary();
//aftp.openportdataconnection();
a = "连接主机:"+hostname+"成功!";
system.out.println(a);
}
catch(ftploginexception e){
a="登陆主机:"+hostname+"失败!请检查用户名或密码是否正确:"+e;
system.out.println(a);
//return false;
}
catch (ioexception e){
a="连接主机:"+hostname+"失败!请检查端口是否正确:"+e;
system.out.println(a);
//return false;
}
catch(securityexception e)
{
a="无权限与主机:"+hostname+"连接!请检查是否有访问权限:"+e;
system.out.println(a);
//return false;
}

log(rwfiledir,a);
return aftp;
}

public void stop(string rwfiledir)
{
string message = "";
try {
if(aftp!=null){
aftp.closeserver();
message = "与主机"+hostname+"连接已断开!";
system.out.println(message);
log(rwfiledir,message);
}
}
catch(ioexception e)
{
message = "与主机"+hostname+"断开连接失败!"+e;
system.out.println(message);
log(rwfiledir,message);
}
}


public boolean downloadfile(string rwfiledir,string filepathname){
boolean result=true;
string message = "";
if (aftp != null)
{
system.out.println("正在下载文件"+filepathname+",请等待....");
string badfile = filepathname.substring(filepathname.length()-4,filepathname.length());
string badlog = filepathname.substring(filepathname.length()-7,filepathname.length());
string baddir = "";
if ((badfile.compareto(".bad") != 0) && (badlog.compareto(".badlog") != 0)){
baddir = "subunsubtosp\\";
}
else{
baddir = "bad\\";
}
string strdir = "subunsubtosp\\";
//system.out.println(rwfiledir + baddir + filepathname);
try{
//ftpclient fc=new ftpclient("192.168.0.56",2121);
//fc.login("lee","lee");
int ch;
file fi = new file(rwfiledir + baddir + filepathname);
//aftp.cd(strdir);
randomaccessfile getfile = new randomaccessfile(fi,"rw");
getfile.seek(0);
telnetinputstream fget=aftp.get(strdir+filepathname);
datainputstream puts = new datainputstream(fget);
while ((ch = puts.read()) >= 0) {
getfile.write(ch);

}
//s.delete();

fget.close();
getfile.close();
//fc.closeserver();

message = "下载"+filepathname+"文件到"+baddir +"目录成功!";
system.out.println(message);
log(rwfiledir,message);
}
catch(ioexception e){
message = "下载"+filepathname+"文件到"+baddir +"目录失败!"+e;
system.out.println(message);
log(rwfiledir,message);
result = false ;

}
}
else{
result = false;
}
return result;
}


public boolean uploadfile(string rwfiledir,string filepathname){
boolean result=true;
string message = "";
if (aftp != null)
{
system.out.println("正在上传文件"+filepathname+",请等待....");

try{
string fg =new string("\\subunsubfromsp\\");
int index = filepathname.lastindexof(fg);
string filename = filepathname.substring(index+1);
file localfile = new file(filepathname) ;

randomaccessfile sendfile = new randomaccessfile(filepathname,"r");
//
sendfile.seek(0);
//改名上传temp_
filename = filename.substring(0,15)+"temp_"+filename.substring(15,filename.length());
outs = aftp.put(filename);
outputs = new dataoutputstream(outs);
while (sendfile.getfilepointer() < sendfile.length() )
{
ch = sendfile.read();
outputs.write(ch);
}

rename(filename.substring(15,filename.length()),filename.substring(20,filename.length()));
outs.close();
sendfile.close();

message = "上传"+filepathname+"文件成功!";
system.out.println(message);
log(rwfiledir,message);
}
catch(ioexception e){
message = "上传"+filepathname+"文件失败!"+e;
system.out.println(message);
log(rwfiledir,message);
result = false ;

}
}
else{
result = false;
}
return result;
}

public void rename(string oldname,string newname){

//aftp.renameto(oldname,newname);
file old = new file(oldname); //oldname
file new = new file(newname); //newname
//aftp.renameto(new);
//boolean old.renameto(file newname);
//system.out.println(old);
//system.out.println(new);

}

public static void delefile(string rwfiledir) {
//try {
//取得readfile目录下的txt文件
string sdir = rwfiledir + "subunsubfromsp\\";
file fdir = new file(sdir);
string filename = "";
int j = fdir.list().length;

system.out.println(sdir+"目录下要删除的文件数:"+fdir.list().length);
file file;
for(int i=0;i {
//删除subunsubfromsp中的txt文件
filename = rwfiledir + "subunsubfromsp\\" + (fdir.list())[0];
file = new file(filename);
file.delete();
system.out.println("已经成功删除"+filename+"文件!");
}
//}
//catch (ioexception e) {
// system.out.println("删除txt文件错误!");
// e.printstacktrace();
//}
}


public void showfilecontents(string strdir)
{
stringbuffer buf = new stringbuffer();
try {
aftp.cd(strdir);
ins= aftp.list();
while ((ch=ins.read())>=0){
buf.append((char)ch);
}

system.out.println(buf.tostring());

ins.close();
}
catch(ioexception e)
{
}
}



// 返回当前目录的所有文件及文件夹

public arraylist getfilelist() throws ioexception {
bufferedreader dr = new bufferedreader(new inputstreamreader(aftp.list()));
arraylist al = new arraylist();
string s = "";
while ( (s = dr.readline()) != null) {
al.add(s);
}
return al;
}

public void setpath(string path) throws ioexception {
if (aftp == null)
this.path = path;
else {
aftp.cd(path);
}
}

// 返回当前目录的文件名称

public arraylist getnamelist(string rwfiledir) throws ioexception {

bufferedreader dr = new bufferedreader(new inputstreamreader(aftp.namelist("subunsubtosp\\")));
arraylist al = new arraylist();
string s = "";
while ( (s = dr.readline()) != null) {
al.add(s);
s = s.substring(13,s.length());
isfile(s);
downloadfile(rwfiledir,s);
//string strfiledelf = aftp.namelist("subunsubtosp\\");
file filedelf=new file(s);
filedelf.delete();
}
return al;
//system.out.println(al.add(s));
}

// 判断一行文件信息是否为目录

public boolean isdir(string line) {
return ( (string) parseline(line).get(0)).indexof("d") != -1;
}

public boolean isfile(string line) {
return!isdir(line);
}

// 处理getfilelist取得的行信息

private arraylist parseline(string line) {
arraylist s1 = new arraylist();
stringtokenizer st = new stringtokenizer(line, " ");
while (st.hasmoretokens()) {
s1.add(st.nexttoken());
}
return s1;
}

//写消息日志
public static void log(string rwfiledir,string msg)
{
string message = "";
try {
java.text.dateformat df = new java.text.simpledateformat("yyyy-mm-dd hh:mm:ss");
java.text.dateformat dflog = new java.text.simpledateformat("yyyymmdd");
java.util.date date = new java.util.date() ;
string datestr = df.format(new java.util.date()) ;
string datelog = dflog.format(new java.util.date()) ;
//string datelog = datestr.substring(0,10);
//datelog = datelog.replace(-, );

//按日期每天生成一个日志文件
filewriter fwl = new filewriter(rwfiledir + "cmssftp"+datelog+".log",true);
printwriter outl = new printwriter(fwl);
outl.println(datestr + " " + msg);
outl.close();
fwl.close();
}catch (ioexception e) {
message = "写log文件错误!"+e;
e.printstacktrace();
log(rwfiledir,message);
system.out.println(message);
}
}
}




文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·qq宠物领养成长完全手则
·用C#读写ini文件-.NET教程,C#语言
·jsp生成xml文件示例-JSP教程,Jsp/Servlet
·检查IP地址的正则表达式-JSP教程,资料/其它
·关于免费申请6位qq的真相
·教你一招免费获得qq秀合影的方法
·用qq进行多人视频聊天绝招
·为DataGrid添加确认删除的对话框-.NET教程,数据库应用
·漫谈Java程序设计中的接口应用-JSP教程,Java技巧及代码
·java做的比较完善的FTP连接上传下载文件-JSP教程,Java技巧及代码
最新文章
·准确识别qq盗号陷阱的方法_qq安全
·解决qq无法接收显示自定义表情的问题_qq技巧
·个人网站定位、盈利的思考_网赚技巧
·photoshop照片合成:简单九步做雪地马群_photoshop教程
·qq2007中简单设置 让你的qq彻底隐藏_qq技巧
·qq2007 beta4发布 qq表情等功能改进_qq技巧
·google adsense建议与技巧_网赚技巧
·美女站长kelly—我的站,就是我的家_站长访谈
·防止qq密码被盗的十项技巧_qq安全
·网站投资你和我的20个自身检查(1)_站长心得
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭