FTP文件的上传下载.docx
《FTP文件的上传下载.docx》由会员分享,可在线阅读,更多相关《FTP文件的上传下载.docx(13页珍藏版)》请在冰豆网上搜索。
FTP文件的上传下载
packagecom.why.ftp;
importjava.io.DataInputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.StringTokenizer;
import.TelnetInputStream;
import.TelnetOutputStream;
import.ftp.FtpClient;
/**
*ftp上传,下载
*@authorwhy2009-07-30
*
*/
publicclassFtpUtil{
privateStringip="";
privateStringusername="";
privateStringpassword="";
privateintport=-1;
privateStringpath="";
FtpClientftpClient=null;
OutputStreamos=null;
FileInputStreamis=null;
publicFtpUtil(StringserverIP,Stringusername,Stringpassword){
this.ip=serverIP;
this.username=username;
this.password=password;
}
publicFtpUtil(StringserverIP,intport,Stringusername,Stringpassword){
this.ip=serverIP;
this.username=username;
this.password=password;
this.port=port;
}
/**
*连接ftp服务器
*
*@throwsIOException
*/
publicbooleanconnectServer(){
ftpClient=newFtpClient();
try{
if(this.port!
=-1){
ftpClient.openServer(this.ip,this.port);
}else{
ftpClient.openServer(this.ip);
}
ftpClient.login(this.username,this.password);
if(this.path.length()!
=0){
ftpClient.cd(this.path);//path是ftp服务下主目录的子目录
}
ftpClient.binary();//用2进制上传、下载
System.out.println("已登录到\""+ftpClient.pwd()+"\"目录");
returntrue;
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}
}
/**
*断开与ftp服务器连接
*
*@throwsIOException
*/
publicbooleancloseServer(){
try{
if(is!
=null){
is.close();
}
if(os!
=null){
os.close();
}
if(ftpClient!
=null){
ftpClient.closeServer();
}
System.out.println("已从服务器断开");
returntrue;
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}
}
/**
*检查文件夹在当前目录下是否存在
*@paramdir
*@return
*/
privatebooleanisDirExist(Stringdir){
Stringpwd="";
try{
pwd=ftpClient.pwd();
ftpClient.cd(dir);
ftpClient.cd(pwd);
}catch(Exceptione){
returnfalse;
}
returntrue;
}
/**
*在当前目录下创建文件夹
*@paramdir
*@return
*@throwsException
*/
privatebooleancreateDir(Stringdir){
try{
ftpClient.ascii();
StringTokenizers=newStringTokenizer(dir,"/");//sign
s.countTokens();
StringpathName=ftpClient.pwd();
while(s.hasMoreElements()){
pathName=pathName+"/"+(String)s.nextElement();
try{
ftpClient.sendServer("MKD"+pathName+"\r\n");
}catch(Exceptione){
e=null;
returnfalse;
}
ftpClient.readServerResponse();
}
ftpClient.binary();
returntrue;
}catch(IOExceptione1){
e1.printStackTrace();
returnfalse;
}
}
/**
*ftp上传
*如果服务器段已存在名为filename的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换
*
*@paramfilename要上传的文件(或文件夹)名
*@return
*@throwsException
*/
publicbooleanupload(Stringfilename){
Stringnewname="";
if(filename.indexOf("/")>-1){
newname=filename.substring(filename.lastIndexOf("/")+1);
}else{
newname=filename;
}
returnupload(filename,newname);
}
/**
*ftp上传
*如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换
*
*@paramfileName要上传的文件(或文件夹)名
*@paramnewName服务器段要生成的文件(或文件夹)名
*@return
*/
publicbooleanupload(StringfileName,StringnewName){
try{
Stringsavefilename=newString(fileName.getBytes("ISO-8859-1"),"GBK");
Filefile_in=newFile(savefilename);//打开本地待长传的文件
if(!
file_in.exists()){
thrownewException("此文件或文件夹["+file_in.getName()+"]有误或不存在!
");
}
if(file_in.isDirectory()){
upload(file_in.getPath(),newName,ftpClient.pwd());
}else{
uploadFile(file_in.getPath(),newName);
}
if(is!
=null){
is.close();
}
if(os!
=null){
os.close();
}
returntrue;
}catch(Exceptione){
e.printStackTrace();
System.err.println("ExceptioneinFtpupload():
"+e.toString());
returnfalse;
}finally{
try{
if(is!
=null){
is.close();
}
if(os!
=null){
os.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
/**
*真正用于上传的方法
*@paramfileName
*@paramnewName
*@parampath
*@throwsException
*/
privatevoidupload(StringfileName,StringnewName,Stringpath)throwsException{
Stringsavefilename=newString(fileName.getBytes("ISO-8859-1"),"GBK");
Filefile_in=newFile(savefilename);//打开本地待长传的文件
if(!
file_in.exists()){
thrownewException("此文件或文件夹["+file_in.getName()+"]有误或不存在!
");
}
if(file_in.isDirectory()){
if(!
isDirExist(newName)){
createDir(newName);
}
ftpClient.cd(newName);
FilesourceFile[]=file_in.listFiles();
for(inti=0;i if(!
sourceFile[i].exists()){
continue;
}
if(sourceFile[i].isDirectory()){
this.upload(sourceFile[i].getPath(),sourceFile[i].getName(),path+"/"+newName);
}else{
this.uploadFile(sourceFile[i].getPath(),sourceFile[i].getName());
}
}
}else{
uploadFile(file_in.getPath(),newName);
}
ftpClient.cd(path);
}
/**
* upload上传文件
*
*@paramfilename要上传的文件名
*@paramnewname上传后的新文件名
*@return-1文件不存在>=0成功上传,返回文件的大小
*@throwsException
*/
publiclonguploadFile(Stringfilename,Stringnewname)throwsException{
longresult=0;
TelnetOutputStreamos=null;
FileInputStreamis=null;
try{
java.io.Filefile_in=newjava.io.File(filename);
if(!
file_in.exists())
return-1;
os=ftpClient.put(newname);
result=file_in.length();
is=newFileInputStream(file_in);
byte[]bytes=newbyte[1024];
intc;
while((c=is.read(bytes))!
=-1){
os.write(bytes,0,c);
}
}finally{
if(is!
=null){
is.close();
}
if(os!
=null){
os.close();
}
}
returnresult;
}
/**
*从ftp下载文件到本地
*
*@paramfilename服务器上的文件名
*@paramnewfilename本地生成的文件名
*@return
*@throwsException
*/
publiclongdownloadFile(Stringfilen