Java网络文件传输的实现—Socket编程
Java网络文件传输的实现——Socket编程本程序分为服务器和客户端两个小程序。
主要实现的功能是:
客户端向服务器端请求一个文件的传输连接,
服务器接收到请求命令后,要求客户端发送用户名和密码,
如果正确,就执行传输文件的过程,客户端接收完后,
保存到一个文件中并在屏幕上显示出来。
设计原理:
服务器端建立一个SocketServer等待客户端的连接,每来一个客户端的请求连接,就创建一个新的线程对其进行单独服务,然后继续等待其他的客户端的连接。创建一个服务器文件server.txt。
客户端向服务器发送文件传输请求,在服务器端要求鉴权时,输入用户名和密码发送给服务器验证,如果验证通过就开始文件传输。
使用方法,如果需要重新编译的话输入以下两条命令:
javac SendFileSocket.java
javac SendFileClient.java
在命令行下的运行方式:
服务器端:
javac SendFileSocket.java
java SendFileSocket
客户端:
javac SendFileClient.java
java SendFileClient serverIPAddress
例如: java SendFileClient 192.168.1.101
用户名和密码:cyf@123
服务器端SendFileSocket.java:
//package cyf.socket;
import https://www.360docs.net/doc/c55366920.html,.*;
import java.io.*;
/****************************
* 一个简单的多线程服务器程序,用于传输文件
*
* @author cyf
****************************/
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 = "cyf";
private static final String password = "123";
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 and password:"+revStr);
if ( revStr.startsWith( "cyf@123" ) )
{
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( );
}
}
客户端SendFileClient.java:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import https://www.360docs.net/doc/c55366920.html,.InetAddress;
import https://www.360docs.net/doc/c55366920.html,.InetSocketAddress;
import https://www.360docs.net/doc/c55366920.html,.Socket;
public class SendFileClient
{
private static final int Server_PORT = 6000;
private static final int Client_PORT = 6001;
/***********************************************************
* 使用方法:运行这个程序需要带上参数,参数类型为点分十进制的ip地址,例如:192.168.1.101
* @param args
* @throws IOException
***************************************************************/
public static void main( String[] args ) throws IOException
{
// TODO Auto-generated method stub
System.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
{
// 创建一个Socket
Socket 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 );// 从输入流中读取数据到buf
System.out.println( new String( buf, 0, len ) );
// 向输出流中写入数据,请求传输一个文件
os.write( "get server.txt".getBytes( ) );
len = is.read( buf );// 从输入流中读取数据到buf
String 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;
}
}
}
运行效果图:
服务端:
客户端:
运行结束后客户端多出一个收到的文件clientread.txt