《java网络文件传输的实现》由会员分享,可在线阅读,更多相关《java网络文件传输的实现(11页珍藏版)》请在金锄头文库上搜索。
1、java网络文件传输的实现Socket 编程All Rights Reserved!本程序分为服务器和客户端两个小程序。主要实现的功能是:客户端向服务器端请求一个文件的传输连接, 服务器接收到请求命令后,要求客户端发送用户名和密码, 如果正确,就执行传输文件的过程,客户端接收完后, 保存到一个文件中并在屏幕上显示岀来。设计原理:服务器端建立一个Socketserver 等待客户端的连接,每来一个客户端的请求连接,就创建一个新的线程对其进行单独服务, 然后继续等待其他的客户端的连接。客户端向服务器发送文件传输请求,在服务器端要求鉴权时, 输入用户名和密码发送给服务器验证,如果验证通过就开始文件传
2、输。使用方法,如果需要重新编译的话输入以下两条命令:javac SendFileSocket.javajavac SendFileClient.java在命令行下的运行方式:服务器端:java SendFileSocket客户端:java SendFileClient serverIPAddress例如:java SendFileClient 192.168.0.153服务器程序:/package zieckey.socket;import java . net .*;import java . io .*;* 一个简单的多线程服务器程序,用于传输文件* author zieckeyThrea
3、dargs )*/public class SendFileSocket extends/* param args*/public static void main ( String/*if ( args.length 0 ) /如果有参数输入就启动服务器程序server(); else/否则启动客户端进程client();*/server ( ); /启动服务器程序privatestaticfinalintPORT=6000 ;privateSocketsprivatestaticfinalStringname=zieckeyprivatestaticfinalStringpassword=
4、123456public SendFileSocket ( Socket s ) this . s = s ;public void run ()tryOutputstream os = s . getOutputStream ();Inputstream is = s . getInputStream ();os. write ( Hello,welcome you! . getBytes ();byte buf = new byte 100;while ( true )int len = is . read ( buf );String revStr = new String ( buf
5、, 0 , len );System. out. println ( This client wants to +revStr );String fileName ;if ( revStr . startsWith ( get ) )/表明客户端请求传输一个文件os. write ( Please input your name and password! Using theformat:namepassword.getBytes ();fileName= getFileName ( revStr );len= is . read ( buf );revStr= new String ( bu
6、f , 0 , len );+revStr );System. out. println ( The received user name and password:if ( revStr . startsWith ( zieckey123456);FileInputStream fins = new FileInputStream ( fileNamebyte fielBuf = new byte100;int data ;while(-1 != ( data = fins . read ( ) ) ) /从文件中读取数据,每次读取1字节os.write ( data ); /将读取到的数据
7、写到网络数据流中发送给客户段break;ososiselse.writegeting files usage is:get filename.getBytes ();.close.close););.close ();catch ( Exception.printStackTrace();/* 作用:从客户端发来了文件请求命令中提取出所请求的文件名开头* 参数:客户端发来了文件请求命令字符串,应该以“ get ”* 返回值:提取岀所请求的文件名*/private String getFileName ( String revStr )StringfileName ;fileName=revSt
8、r.substring(3 );while(fileName ,.startsWith(“)fileName=fileName . substring ( 1returnfileName ;public static void server ()System. out. println ( This is server );tryServerSocket ss = new ServerSocket ( PORT); int count = 0;while ( true )/创建一个Socket等待客户端连接Socket s = ss . accept (); count + ;);System
9、, out. println ( This is the+ count + st client connetion!new SendFileSocket ( s ). start ( ); /启动一个线程为这个客户端服务 catch ( Exception ex )ex. printStackTrace ();/*public static void client()System.out.println( This is client);try/创建一个SocketSocket s = new Socket( lnetAddress.getByName( null ), PORT );Outp
10、utStream os = s.getOutputStream( );/输出流InputStream is = s.getlnputStream( );/输入流byte buf = new byte1OO;int len = is.read( buf );/从输入流中读取数据到bufSystem.out.println( new String( buf, 0, len );/向输岀流中写入数据,请求传输一个文件os.write( get server.txt.getBytes();len = is.read( buf );/从输入流中读取数据到bufString tempStr = new S
11、tring(buf,0,len);if ( tempStr.startsWith( Please input your name and password)System.out.print(Please input your name and password,);System.out.print(Using the format:namepassword:);System.in.read( buf );os.write( buf );/开始读取文件数据并把它写到一个名为clientread.txt 的文件中FileOutputStream fos = new FileOutputStream
12、( clientread.txt);int data;while ( -1 != ( data = is.read()fos.write( data );System.out.println(nFile has been recerved successfully.);os.close();is.close();s.close(); catch ( Exception ex )ex.printStackTrace();*/客户端程序:importjavaioFileOutputStreamimportjavaiolOException ;importjavaioInputStream ;imp
13、ortjavaioOutputStream ;import java . net. InetAddress ;import java . net. InetSocketAddress ;import java . net. Socket;public class SendFileClientprivatestaticfinalintServer_PORT= 6000 ;privatestaticfinalintClient_PORT= 6001 ;/* 使用方法:运行这个程序需要带上参数,参数类型为点分十进制的ip地址,例如:192.168.0.153* param args* throws lOException*/public static void main ( String args ) throws lOException/ TODO Auto-generated method stubSystem. out. println ( This is client );/*System.out.print(Please input your name and password,);System.out.print(Using the format:namepassword:);byte buf = new byte