资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
java网络文件传输的实现Socket编程All Rights Reserved! 本程序分为服务器和客户端两个小程序。主要实现的功能是:客户端向服务器端请求一个文件的传输连接,服务器接收到请求命令后,要求客户端发送用户名和密码,如果正确,就执行传输文件的过程,客户端接收完后,保存到一个文件中并在屏幕上显示出来。设计原理:服务器端建立一个SocketServer等待客户端的连接,每来一个客户端的请求连接,就创建一个新的线程对其进行单独服务,然后继续等待其他的客户端的连接。客户端向服务器发送文件传输请求,在服务器端要求鉴权时,输入用户名和密码发送给服务器验证,如果验证通过就开始文件传输。使用方法,如果需要重新编译的话输入以下两条命令: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 zieckey*/public class SendFileSocket extends Thread/* * param args */public static void main( String args )/*if ( args.length 0 ) / 如果有参数输入就启动服务器程序server( ); else/ 否则启动客户端进程client( );*/server( );/启动服务器程序private static final intPORT= 6000;private Sockets;private static final Stringname= zieckey;private static final Stringpassword= 123456;public 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 byte100;while ( true )int len = is.read( buf );String revStr = new String( buf, 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 the format:namepassword.getBytes( ) );fileName = getFileName( revStr );len = is.read( buf );revStr = new String( buf, 0, len );System.out.println( The received user name and password:+revStr);if ( revStr.startsWith( zieckey123456 ) )FileInputStream fins = new FileInputStream( fileName );/byte fielBuf = new byte100;int data;while ( -1 != ( data = fins.read( ) ) )/从文件中读取数据,每次读取1字节os.write( data ); /将读取到的数据写到网络数据流中发送给客户段break; elseos.write( geting files usage is:get filename.getBytes( ) );os.close( );is.close( );s.close( ); catch ( Exception e )e.printStackTrace( );/* * 作用:从客户端发来了文件请求命令中提取出所请求的文件名 * 参数:客户端发来了文件请求命令字符串,应该以“get ”开头 * 返回值:提取出所请求的文件名 */private String getFileName( String revStr )String fileName;fileName = revStr.substring( 3 );while ( fileName.startsWith( ) )fileName = fileName.substring( 1 );return fileName;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.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( InetAddress.getByName( null ), PORT );OutputStream os = s.getOutputStream( );/ 输出流InputStream is = s.getInputStream( );/ 输入流byte buf = new byte100;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 String(buf,0,len);if ( tempStr.startsWith( Please input your name and password ) )
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号