|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
最后我再次声明,我并没有说不看好java,实际上我对java很乐观的,毕竟她正在不断改进中,我相信她总有一天会和.net并驾齐驱的对照|上传|下载这是偶第一次写java程序完成公司请求的FTP数据同步,一切原代码以下,以供列位参考并提出可贵定见!别的俺另有两个功效没有完成,有请妙手指导一二:1、怎样删除FTP服务器上已下载过的文件?2、怎样将上传的文件更名?上传时在文件名前加“temp_”,上传终了后必要把文件名改返来。3、怎样利用openPassiveDataConnection()办法将ftp毗连变动为自动毗连,由于要从unix体系设置的ftp服务器下载文件的话,就必要自动毗连。
/*
*******************************************************************************************************
Filename:ftp.java
Author:leetsing(elove)
Createdate:2004-08-30
Use:connecttoFTPserver,thenuploadanddownloadfile
Modifydate:2004-09-05addtouploadfile
2004-09-13addtodownloadfile
Copyright:MagiskyMediaTechnologyCo.,Ltd.
*******************************************************************************************************
*/
//importcz.dhl.io.*;
//importcz.dhl.ftp.*;
importsun.net.ftp.*;
importsun.net.*;
importjava.applet.*;
importjava.io.*;
importjava.io.IOException;
importjava.util.StringTokenizer;
importsun.net.ftp.FtpClient;
importjava.util.ArrayList;
publicclassftpextendsApplet
{
FtpClientaftp;
DataOutputStreamoutputs;
TelnetInputStreamins;
TelnetOutputStreamouts;
intch;
publicStringa;
Stringhostname="";
privateStringpath="/";
publicstaticvoidmain(String[]args)
{
Stringhostname="192.168.0.56";
intport=2121;
Stringuid="lee";
Stringpwd="lee";
StringRWFileDir="D:smsftp";//文件目次
//毗连ftp服务器
ftpft=newftp();
ft.connect(RWFileDir,hostname,port,uid,pwd);
//下载文件
if(ft.aftp!=null){
try{
ft.getNameList(RWFileDir);
}catch(IOExceptione)
{
System.out.println("下载文件堕落:"+e);
}
}
//上传文件
if(ft.aftp!=null){
Stringsdir=RWFileDir+"subunsubfromsp";
Filefdir=newFile(sdir);
StringFileName="";
for(inti=0;iFileName=sdir+(fdir.list())[i];
ft.uploadFile(RWFileDir,FileName);
}
//System.out.println("乐成上传的文件:");
//ft.showFileContents("subunsubfromsp");
}
//删除subunsubfromsp目次下已上传的文件文件
//ft.deleFile(RWFileDir);
//断开服务器毗连
ft.stop(RWFileDir);
}
publicFtpClientconnect(StringRWFileDir,Stringhostname,intport,Stringuid,Stringpwd)
{
this.hostname=hostname;
System.out.println("正在毗连"+hostname+",请守候.....");
try{
aftp=newFtpClient(hostname,port);
aftp.login(uid,pwd);
aftp.binary();
//aftp.openPortDataConnection();
a="毗连主机:"+hostname+"乐成!";
System.out.println(a);
}
catch(FtpLoginExceptione){
a="上岸主机:"+hostname+"失利!请反省用户名或暗码是不是准确:"+e;
System.out.println(a);
//returnfalse;
}
catch(IOExceptione){
a="毗连主机:"+hostname+"失利!请反省端口是不是准确:"+e;
System.out.println(a);
//returnfalse;
}
catch(SecurityExceptione)
{
a="无权限与主机:"+hostname+"毗连!请反省是不是有会见权限:"+e;
System.out.println(a);
//returnfalse;
}
log(RWFileDir,a);
returnaftp;
}
publicvoidstop(StringRWFileDir)
{
Stringmessage="";
try{
if(aftp!=null){
aftp.closeServer();
message="与主机"+hostname+"毗连已断开!";
System.out.println(message);
log(RWFileDir,message);
}
}
catch(IOExceptione)
{
message="与主机"+hostname+"断开毗连失利!"+e;
System.out.println(message);
log(RWFileDir,message);
}
}
publicbooleandownloadFile(StringRWFileDir,Stringfilepathname){
booleanresult=true;
Stringmessage="";
if(aftp!=null)
{
System.out.println("正鄙人载文件"+filepathname+",请守候....");
Stringbadfile=filepathname.substring(filepathname.length()-4,filepathname.length());
Stringbadlog=filepathname.substring(filepathname.length()-7,filepathname.length());
Stringbaddir="";
if((badfile.compareTo(".bad")!=0)&&(badlog.compareTo(".badlog")!=0)){
baddir="subunsubtosp";
}
else{
baddir="bad";
}
Stringstrdir="subunsubtosp";
//System.out.println(RWFileDir+baddir+filepathname);
try{
//FtpClientfc=newFtpClient("192.168.0.56",2121);
//fc.login("lee","lee");
intch;
Filefi=newFile(RWFileDir+baddir+filepathname);
//aftp.cd(strdir);
RandomAccessFilegetFile=newRandomAccessFile(fi,"rw");
getFile.seek(0);
TelnetInputStreamfget=aftp.get(strdir+filepathname);
DataInputStreamputs=newDataInputStream(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(IOExceptione){
message="下载"+filepathname+"文件到"+baddir+"目次失利!"+e;
System.out.println(message);
log(RWFileDir,message);
result=false;
}
}
else{
result=false;
}
returnresult;
}
publicbooleanuploadFile(StringRWFileDir,Stringfilepathname){
booleanresult=true;
Stringmessage="";
if(aftp!=null)
{
System.out.println("正在上传文件"+filepathname+",请守候....");
try{
Stringfg=newString("subunsubfromsp");
intindex=filepathname.lastIndexOf(fg);
Stringfilename=filepathname.substring(index+1);
FilelocalFile=newFile(filepathname);
RandomAccessFilesendFile=newRandomAccessFile(filepathname,"r");
//
sendFile.seek(0);
//更名上传temp_
filename=filename.substring(0,15)+"temp_"+filename.substring(15,filename.length());
outs=aftp.put(filename);
outputs=newDataOutputStream(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(IOExceptione){
message="上传"+filepathname+"文件失利!"+e;
System.out.println(message);
log(RWFileDir,message);
result=false;
}
}
else{
result=false;
}
returnresult;
}
publicvoidrename(StringoldName,StringnewName){
//aftp.renameTo(oldName,newName);
FileOld=newFile(oldName);//oldName
FileNew=newFile(newName);//newName
//aftp.renameTo(New);
//booleanOld.renameTo(FilenewName);
//System.out.println(Old);
//System.out.println(New);
}
publicstaticvoiddeleFile(StringRWFileDir){
//try{
//获得ReadFile目次下的txt文件
Stringsdir=RWFileDir+"subunsubfromsp";
Filefdir=newFile(sdir);
StringFileName="";
intj=fdir.list().length;
System.out.println(sdir+"目次下要删除的文件数:"+fdir.list().length);
Filefile;
for(inti=0;i{
//删除subunsubfromsp中的txt文件
FileName=RWFileDir+"subunsubfromsp"+(fdir.list())[0];
file=newFile(FileName);
file.delete();
System.out.println("已乐成删除"+FileName+"文件!");
}
//}
//catch(IOExceptione){
//System.out.println("删除txt文件毛病!");
//e.printStackTrace();
//}
}
publicvoidshowFileContents(Stringstrdir)
{
StringBufferbuf=newStringBuffer();
try{
aftp.cd(strdir);
ins=aftp.list();
while((ch=ins.read())>=0){
buf.append((char)ch);
}
System.out.println(buf.toString());
ins.close();
}
catch(IOExceptione)
{
}
}
//前往以后目次的一切文件及文件夹
publicArrayListgetFileList()throwsIOException{
BufferedReaderdr=newBufferedReader(newInputStreamReader(aftp.list()));
ArrayListal=newArrayList();
Strings="";
while((s=dr.readLine())!=null){
al.add(s);
}
returnal;
}
publicvoidsetPath(Stringpath)throwsIOException{
if(aftp==null)
this.path=path;
else{
aftp.cd(path);
}
}
//前往以后目次的文件称号
publicArrayListgetNameList(StringRWFileDir)throwsIOException{
BufferedReaderdr=newBufferedReader(newInputStreamReader(aftp.nameList("subunsubtosp")));
ArrayListal=newArrayList();
Strings="";
while((s=dr.readLine())!=null){
al.add(s);
s=s.substring(13,s.length());
isFile(s);
downloadFile(RWFileDir,s);
//StringstrFileDelF=aftp.nameList("subunsubtosp");
FilefileDelF=newFile(s);
fileDelF.delete();
}
returnal;
//System.out.println(al.add(s));
}
//判别一行文件信息是不是为目次
publicbooleanisDir(Stringline){
return((String)parseLine(line).get(0)).indexOf("d")!=-1;
}
publicbooleanisFile(Stringline){
return!isDir(line);
}
//处置getFileList获得的行信息
privateArrayListparseLine(Stringline){
ArrayLists1=newArrayList();
StringTokenizerst=newStringTokenizer(line,"");
while(st.hasMoreTokens()){
s1.add(st.nextToken());
}
returns1;
}
//写动静日记
publicstaticvoidlog(StringRWFileDir,Stringmsg)
{
Stringmessage="";
try{
java.text.DateFormatdf=newjava.text.SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
java.text.DateFormatdflog=newjava.text.SimpleDateFormat("yyyyMMdd");
java.util.Datedate=newjava.util.Date();
Stringdatestr=df.format(newjava.util.Date());
Stringdatelog=dflog.format(newjava.util.Date());
//Stringdatelog=datestr.substring(0,10);
//datelog=datelog.replace(-,);
//按日期天天天生一个日记文件
FileWriterfwl=newFileWriter(RWFileDir+"CMSSftp"+datelog+".log",true);
PrintWriteroutl=newPrintWriter(fwl);
outl.println(datestr+""+msg);
outl.close();
fwl.close();
}catch(IOExceptione){
message="写log文件毛病!"+e;
e.printStackTrace();
log(RWFileDir,message);
System.out.println(message);
}
}
}
多谢指点,其实我对.net和ruby也不是很了解,对与java也只是刚起步的阶段,只是在学习中有了点想法就写出来了,现在俺本科还没毕业,所以对大型项目基本上也没有什么经验。 |
|