ftpjava源代码 ftp源码分析-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

ftpjava源代码 ftp源码分析

诸位大神谁有java 实现FTP客户端的源码

您好,/ **

成都创新互联公司长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为清原企业提供专业的成都网站设计、成都网站制作、外贸网站建设清原网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。

*创建日期:2008年12月23日

*类名:

*类路径:组织结构

*更改日志:

* / 包组织结构;

进口的java.io.File;

进口java.io.FileInputStream中;

进口java.io.FileOutputStream中;

进口的java。 io.IOException;

进口sun.net.TelnetInputStream;

进口sun.net.TelnetOutputStream;

进口sun.net.;

/ **

* @作者南山地狱

* @说明FTP操作

* /

公共类的Ftp {

/ **

* BR /获取FTP目录* / 公共无效getftpList(){

字符串服务器=“IP地址 /输入FTP服务器/弦乐用户=”“;/ / FTP服务器的登录用户名

字符串密码=“”;/ /登录FTP服务器的用户名

字符串路径密码=“”;/ / FTP路径上的服务器

尝试{

FtpClient的FTP客户端=新FtpClient的();/ /创建FtpClient的对象

ftpClient.openServer(服务器);/ /连接到FTP服务器

ftpClient.login(用户名,密码);/ / FTP服务器 BR /如果(path.length()= 0){

ftpClient.cd(路径);

}

TelnetInputStream是= ftpClient.list();

诠释三;

而{

System.out.print((char)的C)((C = is.read())= -1!);

}

掉} is.close ();

ftpClient.closeServer();/ /退出FTP服务器

}赶上(IOException异常前){

System.out.println(ex.getMessage());

}

}

/ **

*

* /

公共无效getFtpFile(){

字符串服务器=“”;/ / IP地址中输入FTP服务器

弦乐用户=“”;/ / FTP服务器的登录用户名

字符串密码=“”;/ /登录密码为FTP服务器的用户名

字符串路径=“路径

字符串文件名“;/ /上=的FTP服务器”“;/ /下载文件名称

尝试{

FtpClient的FTP客户端=新FtpClient的();

ftpClient.openServer(服务器);

ftpClient.login(用户名,密码);

如果(路径。长度()= 0)

ftpClient.cd(路径);!

ftpClient.binary();

TelnetInputStream是= ftpClient.get(文件名);

文件file_out =新的文件(文件名);

文件输出流OS =新的文件输出流(file_out);

字节[]字节=新字节[1024];

诠释三;

而((C = is.read(字节))= -1){

os.write (字节,0,C);

}!

掉} is.close();

os.close();

ftpClient.closeServer();

}赶上(IOException异常前){

System.out.println (ex.getMessage());

}

FTP}

/ **

*文件上传到FTP

* /

公共无效putFtpFile() {

字符串服务器=“”;/ /输入IP地址对服务器

字符串用户的地址=“”;/ / FTP服务器的登录用户名

字符串密码=“”;/ / FTP服务器登录用户名密码

字符串路径=“”就 / FTP服务器/字符串文件名=“”;/ /上传的文件名

FtpClient的FTP客户端=新的try { FtpClient的();

ftpClient.openServer(服务器);

ftpClient.login(用户名,密码);

如果(!path.length()= 0)

ftpClient.cd (路径);

ftpClient.binary();

TelnetOutputStream OS = ftpClient.put(文件名);

文件file_in =新的文件(文件名);

文件输入流是=新的文件输入流(file_in);

字节[]字节=新字节[1024];

诠释三;

同时(! (C = is.read(字节))= -1){

操作系统。写(字节,0,C);

}

掉} is.close();

os.close();

ftpClient.closeServer();

}赶上(IOException异常前){

System.out.println(ex.getMessage());

}

}

}

求FTP Server和FTP Client的源代码,要java或C++的,

客户端

/* Client side of an ftp service. Actions:

- connect to server and request service

- send size-of-sile info to server

- start receiving file from server

- close connection

*/

#include stdio.h

#include sys/types.h

#include sys/socket.h

#include netinet/in.h

#include arpa/inet.h

#include time.h

#include errno.h

#define SERVER_PORT_ID 6081

#define CLIENT_PORT_ID 6086

#define SERVER_HOST_ADDR "128.119.40.186"

/* gaia.cs.umass.edu */

#define MAXSIZE 512

#define ACK 2

#define NACK 3

#define REQUESTFILE 100

#define COMMANDNOTSUPPORTED 150

#define COMMANDSUPPORTED 160

#define BADFILENAME 200

#define FILENAMEOK 400

#define STARTTRANSFER 500

int readn(int sd,char *ptr,int size);

int writen(int sd,char *ptr,int size);

main(int argc,char *argv[])

{

int sockid, newsockid,i,getfile,ack,msg,msg_2,c,len;

int no_writen,start_xfer, num_blks,num_last_blk;

struct sockaddr_in my_addr, server_addr;

FILE *fp;

char in_buf[MAXSIZE];

if(argc != 2) {printf("error: usage : sftp filename\n"); exit(0);}

no_writen = 0;

num_blks = 0;

num_last_blk = 0;

len = strlen(argv[1]);

printf("client: creating socket\n");

if ((sockid = socket(AF_INET,SOCK_STREAM,0)) 0)

{ printf("client: socket error : %d\n", errno); exit(0);

}

printf("client: binding my local socket\n");

bzero((char *) my_addr,sizeof(my_addr));

my_addr.sin_family = AF_INET;

my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

my_addr.sin_port = htons(CLIENT_PORT_ID);

if (bind(sockid ,(struct sockaddr *) my_addr,sizeof(my_addr)) 0)

{printf("client: bind error :%d\n", errno); exit(0);

}

printf("client: starting connect\n");

bzero((char *) server_addr,sizeof(server_addr));

server_addr.sin_family = AF_INET;

server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDR);

server_addr.sin_port = htons(SERVER_PORT_ID);

if (connect(sockid ,(struct sockaddr *) server_addr,

sizeof(server_addr)) 0)

{printf("client: connect error :%d\n", errno); exit(0);

}

/* Once we are here, we've got a connection to the server */

/* tell server that we want to get a file */

getfile = htons(REQUESTFILE);

printf("client: sending command request to ftp server\n");

if((writen(sockid,(char *)getfile,sizeof(getfile))) 0)

{printf("client: write error :%d\n", errno); exit(0);}

/* want for go-ahead from server */

msg = 0;

if((readn(sockid,(char *)msg,sizeof(msg))) 0)

{printf("client: read error :%d\n", errno); exit(0); }

msg = ntohs(msg);

if (msg==COMMANDNOTSUPPORTED) {

printf("client: server refused command. goodbye\n");

exit(0);

}

else

printf("client: server replied %d, command supported\n",msg);

/* send file name to server */

printf("client: sending filename\n");

if ((writen(sockid,argv[1],len)) 0)

{printf("client: write error :%d\n", errno); exit(0);}

/* see if server replied that file name is OK */

msg_2 = 0;

if ((readn(sockid,(char *)msg_2,sizeof(msg_2))) 0)

{printf("client: read error :%d\n", errno); exit(0); }

msg_2 = ntohs(msg_2);

if (msg_2 == BADFILENAME) {

printf("client: server reported bad file name. goodbye.\n");

exit(0);

}

else

printf("client: server replied %d, filename OK\n",msg_2);

/* CLIENT KNOWS SERVER HAS BEEN ABLE TO OPEN THE FILE IN READ

MODE AND IS ASKING FOR GO-AHEAD*/

/* CLIENT NOW OPENS A COPY OF THE FILE IN WRITE MODE AND SENDS

THE GOAHEAD TO SERVER*/

printf("client: sending start transfer command\n");

start_xfer = STARTTRANSFER;

start_xfer = htons(start_xfer);

if ((writen(sockid,(char *)start_xfer,sizeof(start_xfer))) 0)

{printf("client: write error :%d\n", errno); exit(0);

}

if ((fp = fopen(argv[1],"w")) == NULL)

{printf(" client: local open file error \n");exit(0);}

/*NOW THE CLIENT IS READING INFORMATION FROM THE SERVER REGARDING HOW MANY

FULL BLOCKS OF SIZE MAXSIZE IT CAN EXPECT. IT ALSO RECEIVES THE NUMBER

OF BYTES REMAINING IN THE LAST PARTIALLY FILLED BLOCK, IF ANY */

if((readn(sockid,(char *)num_blks,sizeof(num_blks))) 0)

{printf("client: read error on nblocks :%d\n",errno);exit(0);}

num_blks = ntohs(num_blks);

printf("client: server responded: %d blocks in file\n",num_blks);

ack = ACK;

ack = htons(ack);

if((writen(sockid,(char *)ack,sizeof(ack))) 0)

{printf("client: ack write error :%d\n",errno);exit(0);

}

if((readn(sockid,(char *)num_last_blk,sizeof(num_last_blk))) 0)

{printf("client: read error :%d on nbytes\n",errno);exit(0);}

num_last_blk = ntohs(num_last_blk);

printf("client: server responded: %d bytes last blk\n",num_last_blk);

if((writen(sockid,(char *)ack,sizeof(ack))) 0)

{printf("client: ack write error :%d\n",errno);exit(0);

}

/* BEGIN READING BLOCKS BEING SENT BY SERVER */

printf("client: starting to get file contents\n");

for(i= 0; i num_blks; i ++) {

if((readn(sockid,in_buf,MAXSIZE)) 0)

{printf("client: block error read: %d\n",errno);exit(0);}

no_writen = fwrite(in_buf,sizeof(char),MAXSIZE,fp);

if (no_writen == 0) {printf("client: file write error\n");exit(0);}

if (no_writen != MAXSIZE)

{printf("client: file write error : no_writen is less\n");exit(0);}

/* send an ACK for this block */

if((writen(sockid,(char *)ack,sizeof(ack))) 0)

{printf("client: ack write error :%d\n",errno);exit(0);}

printf(" %d...",i);

}

/*IF THERE IS A LAST PARTIALLY FILLED BLOCK, READ IT */

if (num_last_blk 0) {

printf("%d\n",num_blks);

if((readn(sockid,in_buf,num_last_blk)) 0)

{printf("client: last block error read :%d\n",errno);exit(0);}

no_writen = fwrite(in_buf,sizeof(char),num_last_blk,fp);

if (no_writen == 0)

{printf("client: last block file write err :%d\n",errno);exit(0);}

if (no_writen != num_last_blk)

{printf("client: file write error : no_writen is less 2\n");exit(0);}

if((writen(sockid,(char *)ack,sizeof(ack))) 0)

{printf("client :ack write error :%d\n",errno);exit(0);}

}

else printf("\n");

/*FILE TRANSFER ENDS. CLIENT TERMINATES AFTER CLOSING ALL ITS FILES

AND SOCKETS*/

fclose(fp);

printf("client: FILE TRANSFER COMPLETE\n");

close(sockid);

}

/* DUE TO THE FACT THAT BUFFER LIMITS IN KERNEL FOR THE SOCKET MAY BE

REACHED, IT IS POSSIBLE THAT READ AND WRITE MAY RETURN A POSITIVE VALUE

LESS THAN THE NUMBER REQUESTED. HENCE WE CALL THE TWO PROCEDURES

BELOW TO TAKE CARE OF SUCH EXIGENCIES */

int readn(int sd,char *ptr,int size)

{ int no_left,no_read;

no_left = size;

while (no_left 0)

{ no_read = read(sd,ptr,no_left);

if(no_read 0) return(no_read);

if (no_read == 0) break;

no_left -= no_read;

ptr += no_read;

}

return(size - no_left);

}

int writen(int sd,char *ptr,int size)

{ int no_left,no_written;

no_left = size;

while (no_left 0)

{ no_written = write(sd,ptr,no_left);

if(no_written =0) return(no_written);

no_left -= no_written;

ptr += no_written;

}

return(size - no_left);

}

服务端

#include stdio.h

#include sys/types.h

#include sys/socket.h

#include netinet/in.h

#include arpa/inet.h

#include time.h

#include errno.h

#define MY_PORT_ID 6081

#define MAXLINE 256

#define MAXSIZE 512

#define ACK 2

#define NACK 3

#define REQUESTFILE 100

#define COMMANDNOTSUPPORTED 150

#define COMMANDSUPPORTED 160

#define BADFILENAME 200

#define FILENAMEOK 400

int writen(int sd,char *ptr,int size);

int readn(int sd,char *ptr,int size);

main() {

int sockid, newsd, pid, clilen;

struct sockaddr_in my_addr, client_addr;

printf("server: creating socket\n");

if ((sockid = socket(AF_INET,SOCK_STREAM,0)) 0)

{printf("server: socket error : %d\n", errno); exit(0); }

printf("server: binding my local socket\n");

bzero((char *) my_addr,sizeof(my_addr));

my_addr.sin_family = AF_INET;

my_addr.sin_port = htons(MY_PORT_ID);

my_addr.sin_addr.s_addr = htons(INADDR_ANY);

if (bind(sockid ,(struct sockaddr *) my_addr,sizeof(my_addr)) 0)

{printf("server: bind error :%d\n", errno); exit(0); }

printf("server: starting listen \n");

if (listen(sockid,5) 0)

{ printf("server: listen error :%d\n",errno);exit(0);}

while(1==1) {

/* ACCEPT A CONNECTION AND THEN CREATE A CHILD TO DO THE WORK */

/* LOOP BACK AND WAIT FOR ANOTHER CONNECTION */

printf("server: starting accept\n");

if ((newsd = accept(sockid ,(struct sockaddr *) client_addr,

clilen)) 0)

{printf("server: accept error :%d\n", errno); exit(0); }

printf("server: return from accept, socket for this ftp: %d\n",

newsd);

if ( (pid=fork()) == 0) {

/* CHILD PROC STARTS HERE. IT WILL DO ACTUAL FILE TRANSFER */

close(sockid); /* child shouldn't do an accept */

doftp(newsd);

close (newsd);

exit(0); /* child all done with work */

}

/* PARENT CONTINUES BELOW HERE */

close(newsd); /* parent all done with client, only child */

} /* will communicate with that client from now on */

}

求每日定时在服务器的FTP上取数据文件的源码(JAVA)

这个是可以向服务器端发送文字的程序,就是在客户端发送一句hello在服务器也可以接受到hello,这个程序可以修改一下就可以了。具体修改方法是增加一个定时器,然后把字符流改成字节流,现在有点忙,你先研究啊,近两天帮你写写看。

服务器端:

import java.net.*;

import java.io.*;

public class DateServer {

public static void main(String[] args) {

ServerSocket server=null;

try{

server=new ServerSocket(6666);

System.out.println(

"Server start on port 6666...");

while(true){

Socket socket=server.accept();

new SocketHandler(socket).start();

/*

PrintWriter out=new PrintWriter(

new OutputStreamWriter(

socket.getOutputStream()

)

);

out.println(new java.util.Date().toLocaleString());

out.close();

*/

}

}catch(Exception e){

e.printStackTrace();

}finally{

if(server!=null) {

try{

server.close();

}catch(Exception ex){}

}

}

}

}

class SocketHandler extends Thread {

private Socket socket;

public SocketHandler(Socket socket) {

this.socket=socket;

}

public void run() {

try{

PrintWriter out=new PrintWriter(

new OutputStreamWriter(

socket.getOutputStream()

)

);

out.println(

new java.util.Date().

toLocaleString());

out.close();

}catch(Exception e){

e.printStackTrace();

}

}

}

客户端:

package com.briup;

import java.io.*;

import java.net.*;

public class FtpClient {

public static void main(String[] args) {

if(args.length==0) {

System.out.println("Usage:java FtpClient file_path");

System.exit(0);

}

File file=new File(args[0]);

if(!file.exists()||!file.canRead()) {

System.out.println(args[0]+" doesn't exist or can not read.");

System.exit(0);

}

Socket socket=null;

try{

socket=new Socket(args[1],Integer.parseInt(args[2]));

BufferedInputStream in=new BufferedInputStream(

new FileInputStream(file)

);

BufferedOutputStream out=new BufferedOutputStream(

socket.getOutputStream()

);

byte[] buffer=new byte[1024*8];

int i=-1;

while((i=in.read(buffer))!=-1) {

out.write(buffer,0,i);

}

System.out.println(socket.getInetAddress().getHostAddress()+" send file over.");

in.close();

out.close();

}catch(Exception e){

e.printStackTrace();

}finally{

if(socket!=null) {

try{

socket.close();

}catch(Exception ex){}

}

}

}

}

求用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);

}

}}


本文题目:ftpjava源代码 ftp源码分析
分享链接:http://kswsj.cn/article/ddcccdd.html

其他资讯