FTP客户端程序设计(java),第1张

package jingupfile;

import sunnetftp;

import sunnet;

import javaioFileInputStream;

import javaioFileOutputStream;

import javaioByteArrayOutputStream;

import javautilArrayList;

import javautilStringTokenizer;

/

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>

ftpclientsendServer("XMKD /test/bb\r\n"); //执行服务器上的FTP命令<br>

ftpclientreadServerResponse一定要在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

@version 10

/

public class FtpUpfile {

private FtpClient ftpclient;

private String ipAddress;

private int ipPort;

private String userName;

private String PassWord;

/

构造函数

@param ip String 机器IP

@param port String 机器FTP端口号

@param username String FTP用户名

@param password String FTP密码

@throws Exception

/

public FtpUpfile(String ip, int port, String username, String password) throws

Exception {

ipAddress = new String(ip);

ipPort = port;

ftpclient = new FtpClient(ipAddress, ipPort);

//ftpclient = new FtpClient(ipAddress);

userName = new String(username);

PassWord = new String(password);

}

/

构造函数

@param ip String 机器IP,默认端口为21

@param username String FTP用户名

@param password String FTP密码

@throws Exception

/

public FtpUpfile(String ip, String username, String password) throws

Exception {

ipAddress = new String(ip);

ipPort = 21;

ftpclient = new FtpClient(ipAddress, ipPort);

//ftpclient = new FtpClient(ipAddress);

userName = new String(username);

PassWord = new String(password);

}

/

登录FTP服务器

@throws Exception

/

public void login() throws Exception {

ftpclientlogin(userName, PassWord);

}

/

退出FTP服务器

@throws Exception

/

public void logout() throws Exception {

//用ftpclientcloseServer()断开FTP出错时用下更语句退出

ftpclientsendServer("QUIT\r\n");

int reply = ftpclientreadServerResponse(); //取得服务器的返回信息

}

/

在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP

上传文件时保证目录的存在目录格式必须以"/"根目录开头

@param pathList String

@throws Exception

/

public void buildList(String pathList) throws Exception {

ftpclientascii();

StringTokenizer s = new StringTokenizer(pathList, "/"); //sign

int count = scountTokens();

String pathName = "";

while (shasMoreElements()) {

pathName = pathName + "/" + (String) snextElement();

try {

ftpclientsendServer("XMKD " + pathName + "\r\n");

} catch (Exception e) {

e = null;

}

int reply = ftpclientreadServerResponse();

}

ftpclientbinary();

}

/

取得指定目录下的所有文件名,不包括目录名称

分析nameList得到的输入流中的数,得到指定目录下的所有文件名

@param fullPath String

@return ArrayList

@throws Exception

/

public ArrayList fileNames(String fullPath) throws Exception {

ftpclientascii(); //注意,使用字符模式

TelnetInputStream list = ftpclientnameList(fullPath);

byte[] names = new byte[2048];

int bufsize = 0;

bufsize = listread(names, 0, nameslength); //从流中读取

listclose();

ArrayList namesList = new ArrayList();

int i = 0;

int j = 0;

while (i < bufsize /nameslength/) {

//char bc = (char) names;

//Systemoutprintln(i + " " + bc + " : " + (int) names);

//i = i + 1;

if (names == 10) { //字符模式为10,二进制模式为13

//文件名在数据中开始下标为j,i-j为文件名的长度,文件名在数据中的结束下标为i-1

//Systemoutwrite(names, j, i - j);

//Systemoutprintln(j + " " + i + " " + (i - j));

String tempName = new String(names, j, i - j);

namesListadd(tempName);

//Systemoutprintln(temp);

// 处理代码处

//j = i + 2; //上一次位置二进制模式

j = i + 1; //上一次位置字符模式

}

i = i + 1;

}

return namesList;

}

/

上传文件到FTP服务器,destination路径以FTP服务器的"/"开始,带文件名、

上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖

@param source String

@param destination String

@throws Exception

/

public void upFile(String source, String destination) throws Exception {

buildList(destinationsubstring(0, destinationlastIndexOf("/")));

ftpclientbinary(); //此行代码必须放在buildList之后

TelnetOutputStream ftpOut = ftpclientput(destination);

TelnetInputStream ftpIn = new TelnetInputStream(new

FileInputStream(source), true);

byte[] buf = new byte[204800];

int bufsize = 0;

while ((bufsize = ftpInread(buf, 0, buflength)) != -1) {

ftpOutwrite(buf, 0, bufsize);

}

ftpInclose();

ftpOutclose();

}

/

JSP中的流上传到FTP服务器,

上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖

字节数组做为文件的输入流,此方法适用于JSP中通过

request输入流来直接上传文件在RequestUpload类中调用了此方法,

destination路径以FTP服务器的"/"开始,带文件名

@param sourceData byte[]

@param destination String

@throws Exception

/

public void upFile(byte[] sourceData, String destination) throws Exception {

buildList(destinationsubstring(0, destinationlastIndexOf("/")));

ftpclientbinary(); //此行代码必须放在buildList之后

TelnetOutputStream ftpOut = ftpclientput(destination);

ftpOutwrite(sourceData, 0, sourceDatalength);

// ftpOutflush();

ftpOutclose();

}

/

从FTP文件服务器上下载文件SourceFileName,到本地destinationFileName

所有的文件名中都要求包括完整的路径名在内

@param SourceFileName String

@param destinationFileName String

@throws Exception

/

public void downFile(String SourceFileName, String destinationFileName) throws

Exception {

ftpclientbinary(); //一定要使用二进制模式

TelnetInputStream ftpIn = ftpclientget(SourceFileName);

byte[] buf = new byte[204800];

int bufsize = 0;

FileOutputStream ftpOut = new FileOutputStream(destinationFileName);

while ((bufsize = ftpInread(buf, 0, buflength)) != -1) {

ftpOutwrite(buf, 0, bufsize);

}

ftpOutclose();

ftpInclose();

}

/

从FTP文件服务器上下载文件,输出到字节数组中

@param SourceFileName String

@return byte[]

@throws Exception

/

public byte[] downFile(String SourceFileName) throws

Exception {

ftpclientbinary(); //一定要使用二进制模式

TelnetInputStream ftpIn = ftpclientget(SourceFileName);

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

byte[] buf = new byte[204800];

int bufsize = 0;

while ((bufsize = ftpInread(buf, 0, buflength)) != -1) {

byteOutwrite(buf, 0, bufsize);

}

byte[] return_arraybyte = byteOuttoByteArray();

byteOutclose();

ftpInclose();

return return_arraybyte;

}

/调用示例

FtpUpfile fUp = new FtpUpfile("19216801", 21, "admin", "admin");

fUplogin();

fUpbuildList("/adfadsg/sfsdfd/cc");

String destination = "/testzip";

fUpupFile("C:\\Documents and Settings\\Administrator\\My Documents\\samplezip",destination);

ArrayList filename = fUpfileNames("/");

for (int i = 0; i < filenamesize(); i++) {

Systemoutprintln(filenameget(i)toString());

}

fUplogout();

@param args String[]

@throws Exception

/

public static void main(String[] args) throws Exception {

FtpUpfile fUp = new FtpUpfile("19215018922", 21, "admin", "admin");

fUplogin();

/ fUpbuildList("/adfadsg/sfsdfd/cc");

String destination = "/test/SetupDJrar";

fUpupFile(

"C:\\Documents and Settings\\Administrator\\My Documents\\SetupDJrar",

destination);

ArrayList filename = fUpfileNames("/");

for (int i = 0; i < filenamesize(); i++) {

Systemoutprintln(filenameget(i)toString());

}

fUpdownFile("/samplezip", "d:\\samplezip");

/

FileInputStream fin = new FileInputStream(

"C:\\AAATXT");

byte[] data = new byte[20480];

finread(data, 0, datalength);

fUpupFile(data, "/test/BBBexe");

fUplogout();

Systemoutprintln("程序运行完成!");

/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

ftpclientsendServer("XMKD /test/bb\r\n"); //执行服务器上的FTP命令

ftpclientreadServerResponse一定要在sendServer后调用

nameList("/test")获取指目录下的文件列表

XMKD建立目录,当目录存在的情况下再次创建目录时报错

XRMD删除目录

DELE删除文件

/

}

}

运用类的办法,编程人员能够长途登录到FTP服务器,罗列该服务器上的目录,设置传输协议,以及传送文件。FtpClient类涵 盖了简直一切FTP的功用,FtpClient的实例变量保留了有关树立"署理"的各种信息。下面给出了这些实例变量:

public static boolean useFtpProxy

这个变量用于标明FTP传输过程中是不是运用了一个署理,因此,它实际上是一个符号,此符号若为TRUE,标明运用了一个署理主机。

public static String ftpProxyHost

此变量只要在变量useFtpProxy为TRUE时才有用,用于保留署理主机名。

public static int ftpProxyPort

此变量只要在变量useFtpProxy为TRUE时才有用,用于保留署理主机的端口地址。

FtpClient有三种不同方式的结构函数,如下所示:

1、public FtpClient(String hostname,int port)

此结构函数运用给出的主机名和端口号树立一条FTP衔接。

2、public FtpClient(String hostname)

此结构函数运用给出的主机名树立一条FTP衔接,运用默许端口号。

3、FtpClient()

此结构函数将创立一FtpClient类,但不树立FTP衔接。这时,FTP衔接能够用openServer办法树立。

一旦树立了类FtpClient,就能够用这个类的办法来翻开与FTP服务器的衔接。类ftpClient供给了如下两个可用于翻开与FTP服务器之间的衔接的办法。

public void openServer(String hostname)

这个办法用于树立一条与指定主机上的FTP服务器的衔接,运用默许端口号。

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » FTP客户端程序设计(java)

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情