首页 > 编程知识 正文

javaftp客户端程序,FTP客户端程序

时间:2023-12-28 21:10:50 阅读:328783 作者:NXIW

本文目录一览:

求用java写一个ftp服务器客户端程序。

import java.io.*;

import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){

String initDir;

initDir = "D:/Ftp";

ServerSocket server;

Socket socket;

String s;

String user;

String password;

user = "root";

password = "123456";

try{

System.out.println("MYFTP服务器启动....");

System.out.println("正在等待连接....");

//监听21号端口

server = new ServerSocket(21);

socket = server.accept();

System.out.println("连接成功");

System.out.println("**********************************");

System.out.println("");

InputStream in =socket.getInputStream();

OutputStream out = socket.getOutputStream();

DataInputStream din = new DataInputStream(in);

DataOutputStream dout=new DataOutputStream(out);

System.out.println("请等待验证客户信息....");

while(true){

s = din.readUTF();

if(s.trim().equals("LOGIN "+user)){

s = "请输入密码:";

dout.writeUTF(s);

s = din.readUTF();

if(s.trim().equals(password)){

s = "连接成功。";

dout.writeUTF(s);

break;

}

else{s ="密码错误,请重新输入用户名:";br dout.writeUTF(s);br br }

}

else{

s = "您输入的命令不正确或此用户不存在,请重新输入:";

dout.writeUTF(s);

}

}

System.out.println("验证客户信息完毕...."); while(true){

System.out.println("");

System.out.println("");

s = din.readUTF();

if(s.trim().equals("DIR")){

String output = "";

File file = new File(initDir);

String[] dirStructure = new String[10];

dirStructure= file.list();

for(int i=0;idirStructure.length;i++){

output +=dirStructure[i]+"n";

}

s=output;

dout.writeUTF(s);

}

else if(s.startsWith("GET")){

s = s.substring(3);

s = s.trim();

File file = new File(initDir);

String[] dirStructure = new String[10];

dirStructure= file.list();

String e= s;

int i=0;

s ="不存在";

while(true){

if(e.equals(dirStructure[i])){

s="存在";

dout.writeUTF(s);

RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");

byte byteBuffer[]= new byte[1024];

int amount;

while((amount = outFile.read(byteBuffer)) != -1){

dout.write(byteBuffer, 0, amount);break;

}break;

}

else if(idirStructure.length-1){

i++;

}

else{

dout.writeUTF(s);

break;

}

}

}

else if(s.startsWith("PUT")){

s = s.substring(3);

s = s.trim();

RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");

byte byteBuffer[] = new byte[1024];

int amount;

while((amount =din.read(byteBuffer) )!= -1){

inFile.write(byteBuffer, 0, amount);break;

}

}

else if(s.trim().equals("BYE"))break;

else{

s = "您输入的命令不正确或此用户不存在,请重新输入:";

dout.writeUTF(s);

}

}

din.close();

dout.close();

in.close();

out.close();

socket.close();

}

catch(Exception e){

System.out.println("MYFTP关闭!"+e);

}

}}

FTP客户端程序设计(java)

package jing.upfile;

import sun.net.ftp.*;

import sun.net.*;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ByteArrayOutputStream;

import java.util.ArrayList;

import java.util.StringTokenizer;

/**

FTP远程命令列表br

USER PORT RETR ALLO DELE SITE XMKD CDUP FEATbr

PASS PASV STOR REST CWD STAT RMD XCUP OPTSbr

ACCT TYPE APPE RNFR XCWD HELP XRMD STOU AUTHbr

REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZbr

QUIT MODE SYST ABOR NLST MKD XPWD MDTM PROTbr

在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上rnbr

ftpclient.sendServer("XMKD /test/bbrn"); //执行服务器上的FTP命令br

ftpclient.readServerResponse一定要在sendServer后调用br

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

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

XRMD删除目录br

DELE删除文件br

* pTitle: 使用JAVA操作FTP服务器(FTP客户端)/p

* pDescription: 上传文件的类型及文件大小都放到调用此类的方法中去检测,比如放到前台JAVASCRIPT中去检测等

* 针对FTP中的所有调用使用到文件名的地方请使用完整的路径名(绝对路径开始)。

* /p

* pCopyright: Copyright (c) 2005/p

* pCompany: 静靖工作室/p

* @author 欧朝敬 13873195792

* @version 1.0

*/

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 {

ftpclient.login(userName, PassWord);

}

/**

* 退出FTP服务器

* @throws Exception

*/

public void logout() throws Exception {

//用ftpclient.closeServer()断开FTP出错时用下更语句退出

ftpclient.sendServer("QUITrn");

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

}

/**

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

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

* @param pathList String

* @throws Exception

*/

public void buildList(String pathList) throws Exception {

ftpclient.ascii();

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

int count = s.countTokens();

String pathName = "";

while (s.hasMoreElements()) {

pathName = pathName + "/" + (String) s.nextElement();

try {

ftpclient.sendServer("XMKD " + pathName + "rn");

} catch (Exception e) {

e = null;

}

int reply = ftpclient.readServerResponse();

}

ftpclient.binary();

}

/**

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

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

* @param fullPath String

* @return ArrayList

* @throws Exception

*/

public ArrayList fileNames(String fullPath) throws Exception {

ftpclient.ascii(); //注意,使用字符模式

TelnetInputStream list = ftpclient.nameList(fullPath);

byte[] names = new byte[2048];

int bufsize = 0;

bufsize = list.read(names, 0, names.length); //从流中读取

list.close();

ArrayList namesList = new ArrayList();

int i = 0;

int j = 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 = new String(names, j, i - j);

namesList.add(tempName);

//System.out.println(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(destination.substring(0, destination.lastIndexOf("/")));

ftpclient.binary(); //此行代码必须放在buildList之后

TelnetOutputStream ftpOut = ftpclient.put(destination);

TelnetInputStream ftpIn = new TelnetInputStream(new

FileInputStream(source), true);

byte[] buf = new byte[204800];

int bufsize = 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服务器的"/"开始,带文件名

* @param sourceData byte[]

* @param destination String

* @throws Exception

*/

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

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

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

* @param SourceFileName String

* @param destinationFileName String

* @throws Exception

*/

public void downFile(String SourceFileName, String destinationFileName) throws

Exception {

ftpclient.binary(); //一定要使用二进制模式

TelnetInputStream ftpIn = ftpclient.get(SourceFileName);

byte[] buf = new byte[204800];

int bufsize = 0;

FileOutputStream ftpOut = new FileOutputStream(destinationFileName);

while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {

ftpOut.write(buf, 0, bufsize);

}

ftpOut.close();

ftpIn.close();

}

/**

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

* @param SourceFileName String

* @return byte[]

* @throws Exception

*/

public byte[] downFile(String SourceFileName) throws

Exception {

ftpclient.binary(); //一定要使用二进制模式

TelnetInputStream ftpIn = ftpclient.get(SourceFileName);

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

byte[] buf = new byte[204800];

int bufsize = 0;

while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {

byteOut.write(buf, 0, bufsize);

}

byte[] return_arraybyte = byteOut.toByteArray();

byteOut.close();

ftpIn.close();

return return_arraybyte;

}

/**调用示例

* FtpUpfile fUp = new FtpUpfile("192.168.0.1", 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();

* @param args String[]

* @throws Exception

*/

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

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

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 = new FileInputStream(

"C:\AAA.TXT");

byte[] data = new byte[20480];

fin.read(data, 0, data.length);

fUp.upFile(data, "/test/BBB.exe");

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命令都要加上rn

ftpclient.sendServer("XMKD /test/bbrn"); //执行服务器上的FTP命令

ftpclient.readServerResponse一定要在sendServer后调用

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

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

XRMD删除目录

DELE删除文件

*/

}

}

如何用java实现ftp客户端程序

FTP 的主要操作都是基于各种命令基础之上的。常用的命令有: · 设置传输模式,它包括ASCⅡ(文本) 和BINARY 二进制模式; · 目录操作,改变或显示远程计算机的当前目录(cd、dir/ls 命令); · 连接操作,open命令用于建立同远程计算机的连接;close命令用于关闭连接; · 发送操作,put命令用于传送文件到远程计算机;mput 命令用于传送多个文件到远程计算机; · 获取操作,get命令用于接收一个文件;mget命令用于接收多个文件。 编程思路 根据FTP 的工作原理,在主函数中建立一个服务器套接字端口,等待客户端请求,一旦客户端请求被接受,服务器程序就建立一个服务器分线程,处理客户端的命令。如果客户端需要和服务器端进行文件的传输,则建立一个新的套接字连接来完成文件的操作。 编程技巧说明

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。