java网络文件传输的实现

  • 格式:doc
  • 大小:102.50 KB
  • 文档页数:11

java网络文件传输的实现——Socket编程All Rights Reserved!本程序分为服务器和客户端两个小程序。

主要实现的功能是:客户端向服务器端请求一个文件的传输连接,服务器接收到请求命令后,要求客户端发送用户名和密码,如果正确,就执行传输文件的过程,客户端接收完后,保存到一个文件中并在屏幕上显示出来。

设计原理:服务器端建立一个SocketServer等待客户端的连接,每来一个客户端的请求连接,就创建一个新的线程对其进行单独服务,然后继续等待其他的客户端的连接。

客户端向服务器发送文件传输请求,在服务器端要求鉴权时,输入用户名和密码发送给服务器验证,如果验证通过就开始文件传输。

使用方法,如果需要重新编译的话输入以下两条命令:javac SendFileSocket.javajavac SendFileClient.java在命令行下的运行方式:服务器端:java SendFileSocket客户端:java SendFileClient serverIPAddress例如:java SendFileClient 192.168.0.153服务器程序:* @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 int PORT= 6000;private Socket s;private static final String name="zieckey";private static final String password ="123456";public SendFileSocket(Socket s ){this.s = s;}public void run(){try{OutputStream 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, 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:name@password".getBytes() );fileName =getFileName( revStr );len = is.read( buf );revStr =new String( buf, 0, len );System.out.println("The received user name andpassword:"+revStr);if( revStr.startsWith("zieckey@123456")){FileInputStream fins =new FileInputStream( fileName );//byte[] fielBuf = new byte[100];int data;while(-1 !=(data =fins.read()))//从文件中读取数据,每次读取1字节{os.write( data );//将读取到的数据写到网络数据流中发送给客户段}break;}}else{os.write("geting file's 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");try{ServerSocket 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 byte[100];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:name@password:");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 SendFileClient{private static final int Server_PORT = 6000;private static final int Client_PORT = 6001;/*** 使用方法:运行这个程序需要带上参数,参数类型为点分十进制的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:name@password:");byte[] buf = new byte[100];System.in.read( buf );*/byte[] buf =new byte[100];byte[]name=new byte[100];//InetAddress inetAddr;if(!isIPAddress(args[0])){System.out.println("The usage is : java SendFileClient ipaddress");System.out.println("For example : java SendFileClient 192.168.0.153");return;}String ipStr = args[0];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:name@password:");do{System.in.read(name);}while(name.length<5 );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;}else{return true;}}。