ImageVerifierCode 换一换
格式:DOC , 页数:11 ,大小:67.50KB ,
资源ID:2563119      下载积分:15 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-2563119.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java网络文件传输的实现.doc)为本站会员(dzzj200808)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

java网络文件传输的实现.doc

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 .*;import java.io.*;/* 一个简单的多线程服务器程序,用于传输文件* * author zieckey*/public class Sen

3、dFileSocket extends Thread/* param args*/public static void main( String args )/*if ( args.length 0 ) / 如果有参数输入就启动服务器程序server( ); else/ 否则启动客户端进程client( );*/server( );/启动服务器程序private static final int PORT = 6000;private Socket s;private static final String name = “zieckey“;private static final Strin

4、g password = “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

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 the format:namepassword“.getBytes( ) );fileName = getFileName( revStr );len = is.read( buf );revStr = new String( buf,

6、 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 ); /将读取到的数据写到网络

7、数据流中发送给客户段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

8、;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.acce

9、pt( );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.get

10、ByName( 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

11、 );/ 从输入流中读取数据到 bufString tempStr = new String(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

12、.txt“的文件中FileOutputStream fos = new FileOutputStream( “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( );*/客户端程序:import java.io.

13、FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .InetAddress;import .InetSocketAddress;import .Socket;public class SendFileClientprivate static final int Server_PORT = 6000;private static final int Client_PORT = 6001;/* 使用方法:运行这个程序需要带上参数,参数类型

14、为点分十进制的 ip 地址,例如:192.168.0.153* param args* throws IOException */public static void main( String args ) throws IOException/ 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:namepasswor

15、d:“);byte buf = new byte100;System.in.read( buf );*/byte buf = new byte100;byte name = new byte100;/InetAddress inetAddr;if ( !isIPAddress(args0) )System.out.println(“The usage is : java SendFileClient ipaddress“);System.out.println(“For example : java SendFileClient 192.168.0.153“);return;String ip

16、Str = args0;try/ 创建一个 SocketSocket s = new Socket();s.connect ( new InetSocketAddress (ipStr , Server_PORT ), Client_PORT );OutputStream os = s.getOutputStream( );/ 输出流InputStream is = s.getInputStream( );/ 输入流int len = is.read( buf );/ 从输入流中读取数据到 bufSystem.out.println( new String( buf, 0, len ) );/

17、 向输出流中写入数据,请求传输一个文件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“ ) )System.out.println(“Please input your name and password, “);System.out.println(“Using the format:n

18、amepassword:“);doSystem.in.read( name ); while ( name.length5 );os.write( name );/开始读取文件数据并把它写到一个名为“clientread.txt“的文件中FileOutputStream fos = new FileOutputStream( “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( );/简单的判断字符串是否为一个 ip 地址/后期再完善该判断方法private static boolean isIPAddress( String ip )if(ip.length( )5)return false; elsereturn true;

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报