JAVA FTP操作
importsun.net.ftp. * ;
importsun.net. * ;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.ByteArrayOutputStream;
importjava.util.ArrayList;
importjava.util.StringTokenizer;
/**
FTP远程命令列表<br>
USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT<br>
PASS PASV STOR REST CWD STAT RMD XCUP OPTS<br>
ACCT TYPE APPE RNFR XCWD HELP XRMD STOU AUTH<br>
REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ<br>
QUIT MODE SYST ABOR NLST MKD XPWD MDTM PROT<br>
在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上\r\n<br>
ftpclient.sendServer("XMKD /test/bb\r\n"); //执行服务器上的FTP命令<br>
ftpclient.readServerResponse一定要在sendServer后调用<br>
nameList("/test")获取指目录下的文件列表<br>
XMKD建立目录,当目录存在的情况下再次创建目录时报错<br>
XRMD删除目录<br>
DELE删除文件<br>
* <p>Title: 使用JAVA操作FTP服务器(FTP客户端)</p>
* <p>Description: 上传文件的类型及文件大小都放到调用此类的方法中去检测,比如放到前台JAVASCRIPT中去检测等
* 针对FTP中的所有调用使用到文件名的地方请使用完整的路径名(绝对路径开始)。
* </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: 静靖工作室</p>
*@author欧朝敬13873195792
*@version1.0
*/
public classFTPConnectorBK{
privateFtpClient ftpclient;
privateString ipAddress;
private intipPort;
privateString userName;
privateString PassWord;
/**
* 构造函数
*@paramip String 机器IP
*@paramport String 机器FTP端口号
*@paramusername String FTP用户名
*@parampassword String FTP密码
*@throwsException
*/
publicFTPConnectorBK(String ip,intport, String username, String password)throws
Exception{
ipAddress= newString(ip);
ipPort=port;
ftpclient= newFtpClient(ipAddress, ipPort);
// ftpclient = new FtpClient(ipAddress);
userName= newString(username);
PassWord= newString(password);
}
/**
* 构造函数
*@paramip String 机器IP,默认端口为21
*@paramusername String FTP用户名
*@parampassword String FTP密码
*@throwsException
*/
publicFTPConnectorBK(String ip, String username, String password)throws
Exception{
ipAddress= newString(ip);
ipPort= 21 ;
ftpclient= newFtpClient(ipAddress, ipPort);
// ftpclient = new FtpClient(ipAddress);
userName= newString(username);
PassWord= newString(password);
}
/**
* 登录FTP服务器
*@throwsException
*/
public voidlogin()throwsException{
ftpclient.login(userName, PassWord);
}
/**
* 退出FTP服务器
*@throwsException
*/
public voidlogout()throwsException{
// 用ftpclient.closeServer()断开FTP出错时用下更语句退出
ftpclient.sendServer( " QUIT\r\n " );
intreply=ftpclient.readServerResponse();// 取得服务器的返回信息
}
/**
* 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP
* 上传文件时保证目录的存在目录格式必须以"/"根目录开头
*@parampathList String
*@throwsException
*/
public voidbuildList(String pathList)throwsException{
ftpclient.ascii();
StringTokenizer s= newStringTokenizer(pathList," / " );// sign
intcount=s.countTokens();
String pathName= "" ;
while(s.hasMoreElements()){
pathName=pathName+ " / " +(String) s.nextElement();
try {
ftpclient.sendServer( " XMKD" +pathName+ " \r\n " );
} catch(Exception e){
e= null ;
}
intreply=ftpclient.readServerResponse();
}
ftpclient.binary();
}
/**
* 取得指定目录下的所有文件名,不包括目录名称
* 分析nameList得到的输入流中的数,得到指定目录下的所有文件名
*@paramfullPath String
*@returnArrayList
*@throwsException
*/
publicArrayList fileNames(String fullPath)throwsException{
ftpclient.ascii();// 注意,使用字符模式
TelnetInputStream list=ftpclient.nameList(fullPath);
byte [] names= new byte [ 2048 ];
intbufsize= 0 ;
bufsize=list.read(names,0 , names.length);// 从流中读取
list.close();
ArrayList namesList= newArrayList();
inti= 0 ;
intj= 0 ;
while(i<bufsize/* names.length */ ){
// char bc = (char) names;
// System.out.println(i + "" + bc + " : " + (int) names);
// i = i + 1;
if(names== 10 ){// 字符模式为10,二进制模式为13
// 文件名在数据中开始下标为j,i-j为文件名的长度,文件名在数据中的结束下标为i-1
// System.out.write(names, j, i - j);
// System.out.println(j + " " + i + " " + (i - j));
String tempName= newString(names, j, i-j);
namesList.add(tempName);
// System.out.println(temp);
//处理代码处
// j = i + 2;// 上一次位置二进制模式
j=i+ 1 ;// 上一次位置字符模式
}
i=i+ 1 ;
}
returnnamesList;
}
/**
* 上传文件到FTP服务器,destination路径以FTP服务器的"/"开始,带文件名、
* 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
*@paramsource String
*@paramdestination String
*@throwsException
*/
public voidupFile(String source, String destination)throwsException{
buildList(destination.substring( 0 , destination.lastIndexOf( " / " )));
ftpclient.binary();// 此行代码必须放在buildList之后
TelnetOutputStream ftpOut=ftpclient.put(destination);
TelnetInputStream ftpIn= newTelnetInputStream( new
FileInputStream(source),true );
byte [] buf= new byte [ 204800 ];
intbufsize= 0 ;
while((bufsize=ftpIn.read(buf,0 , buf.length))!= - 1 ){
ftpOut.write(buf,0 , bufsize);
}
ftpIn.close();
ftpOut.close();
}
/**
* JSP中的流上传到FTP服务器,
* 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
* 字节数组做为文件的输入流,此方法适用于JSP中通过
* request输入流来直接上传文件在RequestUpload类中调用了此方法,
* destination路径以FTP服务器的"/"开始,带文件名
*@paramsourceData byte[]
*@paramdestination String
*@throwsException
*/
public voidupFile( byte [] sourceData, String destination)throwsException{
buildList(destination.substring( 0 , destination.lastIndexOf( " / " )));
ftpclient.binary();// 此行代码必须放在buildList之后
TelnetOutputStream ftpOut=ftpclient.put(destination);
ftpOut.write(sourceData,0 , sourceData.length);
// ftpOut.flush();
ftpOut.close();
}
/**
* 从FTP文件服务器上下载文件SourceFileName,到本地destinationFileName
* 所有的文件名中都要求包括完整的路径名在内
*@paramSourceFileName String
*@paramdestinationFileName String
*@throwsException
*/
public voiddownFile(String SourceFileName, String destinationFileName)throws
Exception{
ftpclient.binary();// 一定要使用二进制模式
TelnetInputStream ftpIn=ftpclient.get(SourceFileName);
byte [] buf= new byte [ 204800 ];
intbufsize= 0 ;
FileOutputStream ftpOut= newFileOutputStream(destinationFileName);
while((bufsize=ftpIn.read(buf,0 , buf.length))!= - 1 ){
ftpOut.write(buf,0 , bufsize);
}
ftpOut.close();
ftpIn.close();
}
/**
*从FTP文件服务器上下载文件,输出到字节数组中
*@paramSourceFileName String
*@returnbyte[]
*@throwsException
*/
public byte [] downFile(String SourceFileName)throws
Exception{
ftpclient.binary();// 一定要使用二进制模式
TelnetInputStream ftpIn=ftpclient.get(SourceFileName);
ByteArrayOutputStream byteOut= newByteArrayOutputStream();
byte [] buf= new byte [ 204800 ];
intbufsize= 0 ;
while((bufsize=ftpIn.read(buf,0 , buf.length))!= - 1 ){
byteOut.write(buf,0 , bufsize);
}
byte [] return_arraybyte=byteOut.toByteArray();
byteOut.close();
ftpIn.close();
returnreturn_arraybyte;
}
/** 调用示例
* FtpUpfile fUp = new FtpUpfile("192.150.189.22", 21, "admin", "admin");
* fUp.login();
* fUp.buildList("/adfadsg/sfsdfd/cc");
* String destination = "/test.zip";
* fUp.upFile("C:\\Documents and Settings\\Administrator\\My Documents\\sample.zip",destination);
* ArrayList filename = fUp.fileNames("/");
* for (int i = 0; i < filename.size(); i++) {
* System.out.println(filename.get(i).toString());
* }
* fUp.logout();
*@paramargs String[]
*@throwsException
*/
public static voidmain(String[] args)throwsException{
FTPConnector fUp= newFTPConnector( " 218.206.76.252 " ,21 ," ftpuser " ," abc123 " );
fUp.login();
/* fUp.buildList("/adfadsg/sfsdfd/cc");
String destination = "/test/SetupDJ.rar";
fUp.upFile(
"C:\\Documents and Settings\\Administrator\\My Documents\\SetupDJ.rar",
destination);
ArrayList filename = fUp.fileNames("/");
for (int i = 0; i < filename.size(); i++) {
System.out.println(filename.get(i).toString());
}
fUp.downFile("/sample.zip", "d:\\sample.zip");
*/
FileInputStream fin= newFileInputStream(
" d:\\OrdSub_20061115133821_401517.xml " );
byte [] data= new byte [ 20480000 ];
fin.read(data,0 , data.length);
fUp.upFile( " d:/OrdSub_20061115133821_401517.xml " ," /OrdSub_20061115133821_401517.xml " );
fUp.logout();
System.out.println( " 程序运行完成! " );
/* FTP远程命令列表
USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT
PASS PASV STOR REST CWD STAT RMD XCUP OPTS
ACCT TYPE APPE RNFR XCWD HELP XRMD STOU AUTH
REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ
QUIT MODE SYST ABOR NLST MKD XPWD MDTM PROT
*/
/* 在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上\r\n
ftpclient.sendServer("XMKD /test/bb\r\n"); //执行服务器上的FTP命令
ftpclient.readServerResponse一定要在sendServer后调用
nameList("/test")获取指目录下的文件列表
XMKD建立目录,当目录存在的情况下再次创建目录时报错
XRMD删除目录
DELE删除文件
*/
}
}
页:
[1]