已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java网络文件传输的实现Socket编程All Rights Reserved! 本程序分为服务器和客户端两个小程序。主要实现的功能是:客户端向服务器端请求一个文件的传输连接,服务器接收到请求命令后,要求客户端发送用户名和密码,如果正确,就执行传输文件的过程,客户端接收完后,保存到一个文件中并在屏幕上显示出来。设计原理:服务器端建立一个SocketServer等待客户端的连接,每来一个客户端的请求连接,就创建一个新的线程对其进行单独服务,然后继续等待其他的客户端的连接。客户端向服务器发送文件传输请求,在服务器端要求鉴权时,输入用户名和密码发送给服务器验证,如果验证通过就开始文件传输。使用方法,如果需要重新编译的话输入以下两条命令:javac SendFileSocket.javajavac SendFileClient.java在命令行下的运行方式:服务器端:java SendFileSocket客户端:java SendFileClient serverIPAddress例如:java SendFileClient 53服务器程序:/package zieckey.socket;import .*;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 ) )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( 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.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .InetAddress;import .InetSocketAddress;import .Socket;public class SendFileClientprivate static final intServer_PORT= 6000;private static final intClient_PORT= 6001;/* * 使用方法:运行这个程序需要带上参数,参数类型为点分十进制的ip地址,例如:53 * 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:namepassword:);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 53);return;String ipStr = 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 ) );/ 向输出流中写入数据,请求传输一个文件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:namepassword:);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( ) ) )f
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- Unit 4 Growing up单元话题书面表达练习(原卷版)-2025-2026学年九年级英语上册(牛津译林版)
- 叙事护理:提升护理人文素养的有效
- 陕西省渭南区解放路中学2026届高二上生物期末复习检测模拟试题含解析
- 单孔腹腔镜手术个案护理
- 山东省青岛平度市2026届物理高一上期末教学质量检测模拟试题含解析
- 西藏自治区日喀则市南木林高中2025-2026学年化学高二第一学期期末经典模拟试题含解析
- 云南省文山州富宁县一中2025-2026学年物理高一上期末教学质量检测模拟试题含解析
- 太原城市职业技术学院《建筑与装饰工程计量与计价课程设计》2024-2025学年第一学期期末试卷
- 血液透析患者动静脉内瘘护理:全流程评估与维护
- Module 7-8-七年级英语下册重点词汇暑假巩固练(外研版)
- 云南省高中学业水平考试数学考题分类汇编以及知识点穿插(2025年7月-2026年1月)
- 手术麻醉科耗材管理制度
- 减糖与健康:科学控糖指南
- 【中国出口信用保险公司】2025稳外贸新兴市场开发指南
- 2025深圳辅警考试真题
- DB11∕T 1200-2023 超长大体积混凝土结构跳仓法技术规程
- fof投资管理制度
- 肝癌介入加靶向治疗讲课件
- 毕业设计(论文)-空气压缩机结构设计
- 2024DBJ33T1095-预拌砂浆应用技术规程
- JG/T 160-2004混凝土用膨胀型、扩孔型建筑锚栓
评论
0/150
提交评论