用JAVA SOCKET发送和接收文件.docx
《用JAVA SOCKET发送和接收文件.docx》由会员分享,可在线阅读,更多相关《用JAVA SOCKET发送和接收文件.docx(13页珍藏版)》请在冰豆网上搜索。
![用JAVA SOCKET发送和接收文件.docx](https://file1.bdocx.com/fileroot1/2022-11/16/7b867f3b-c7a6-472c-9d6d-81286a66b2be/7b867f3b-c7a6-472c-9d6d-81286a66b2be1.gif)
用JAVASOCKET发送和接收文件
用JAVASOCKET发送和接收文件
用JAVASOCKET发送和接收文件
Java代码
/*服务器端接收文件*/
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.RandomAccessFile;
import.ServerSocket;
import.Socket;
importjavax.swing.JFileChooser;
/**
*该类用到的绑定端口初始为10000,如果绑定不成功则试另外的端口;
*绑定次数用tryBindTimes变量,表示如果绑定失败会对它加一;
*当前绑定端口由DefaultBindPort+tryBindTimes决定;
*外界系统(调用此程序的对象)可以获取当前的帮定端口;
*并告诉客户端服务的端口号以使其能正确连接到该端口上;
*@author
*
*/
publicclassGetFileextendsThread{
//服务套接字等待对方的连接和文件发送
ServerSocketserSocket;
//由服务套接字产生的套接字
SockettempSocket;
//用于读取
InputStreaminSocket;
//随机访问文件
RandomAccessFileinFile=null;
//临时缓寸区
bytebyteBuffer[]=newbyte[1024];
//默认用10000端口监听请求
intdefaultBindPort=10000;
//初始的绑定端口次数为0
inttryBindTimes=0;
//当前绑定的端口号是10000默认端口
intcurrentBindPort=defaultBindPort+tryBindTimes;
/**
*@构造方法
*@抛出异常的原因是无法绑定服务的端口
**/
publicGetFile(intport)throwsException{
try{
//绑定服务的端口
this.bindToServerPort();
}catch(Exceptione){
e.printStackTrace();
//绑定不成功重试
System.out.println(e.toString());
thrownewException("绑定端口不成功!
");
}
//文件选择器以当前的目录打开
JFileChooserjfc=newJFileChooser(".");
jfc.showSaveDialog(newjavax.swing.JFrame());
//获取当前的选择文件引用
FilesavedFile=jfc.getSelectedFile();
//已经选择了文件
if(savedFile!
=null){
//读取文件的数据,可以每次以快的方式读取数据
inFile=newRandomAccessFile(savedFile,"rw");
}
}
/**
*@监控线程
*/
publicvoidrun(){
try{
if(this.inFile==null){
System.out.println("没有选择文件");
//关闭服务方套接字
this.serSocket.close();
//没有选择文件
return;
}
System.out.println("waitfor..."+'\n'+"等待对方接入");
//等待对方的连接
tempSocket=serSocket.accept();
//五秒钟连不上将抛出异常
this.serSocket.setSoTimeout(5000);
//获取输入流
this.inSocket=tempSocket.getInputStream();
}catch(Exceptionex){
System.out.println(ex.toString());
ex.printStackTrace();
return;
}
//以下为传送文件代码和套接字清理工作
intamount;
try{
while((amount=inSocket.read(byteBuffer))!
=-1){
inFile.write(byteBuffer,0,amount);
}
//关闭流
inSocket.close();
javax.swing.JOptionPane.showMessageDialog(newjavax.swing.JFrame(),
"已接收成功","提示!
",javax.swing.JOptionPane.PLAIN_MESSAGE);
System.out.println("GetOK");
System.out.println("接收完毕!
");
//关闭文件
inFile.close();
//关闭临时套接字
tempSocket.close();
//关闭服务方套接字
this.serSocket.close();
}catch(IOExceptione){
System.out.println(e.toString());
e.printStackTrace();
}
}
/**
*@绑定端口
*@throwsException抛出异常的原因是无法绑定服务的端口
*/
privatevoidbindToServerPort()throwsException{
try{
//输出绑定的端口号到当前的控制台上
System.out.println("试绑定的端口号是:
"+this.currentBindPort);
//在自己的机器上开一个服务类套接字并等待发送者的连接
serSocket=newServerSocket(this.currentBindPort);
}catch(Exceptione){
e.printStackTrace();
//绑定不成功重试
System.out.println(e.toString());
//试了不止一次了
this.tryBindTimes=this.tryBindTimes+1;
//可查看试的次数getTryBindedTimes
this.currentBindPort=this.defaultBindPort+this.tryBindTimes;
//如果试的次数超过20次退出
if(this.tryBindTimes>=20){
thrownewException("无法绑定到指定端口"+'\n'+"试了太多次了!
");
}
//递归的绑定
this.bindToServerPort();
}
//输出绑定的端口号到当前的控制台上
System.out.println("成功绑定的端口号是:
"+this.currentBindPort);
}
//获取试绑定的端口
publicintgetTryBindedTimes(){
returnthis.tryBindTimes;
}
//获取已经绑定的端口
publicintgetCurrentBindingPort(){
returnthis.currentBindPort;
}
/**
*@测试方法
*@paramargs
*/
publicstaticvoidmain(Stringargs[]){
GetFilegetFile=null;
try{
getFile=newGetFile(10000);
}catch(Exceptione){
e.printStackTrace();
System.out.println("无法传送文件!
");
System.exit
(1);
}
getFile.start();
}
}/*服务器端接收文件*/
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.RandomAccessFile;
import.ServerSocket;
import.Socket;importjavax.swing.JFileChooser;/**
*该类用到的绑定端口初始为10000,如果绑定不成功则试另外的端口;
*绑定次数用tryBindTimes变量,表示如果绑定失败会对它加一;
*当前绑定端口由DefaultBindPort+tryBindTimes决定;
*外界系统(调用此程序的对象)可以获取当前的帮定端口;
*并告诉客户端服务的端口号以使其能正确连接到该端口上;
*@author
*
*/
publicclassGetFileextendsThread{//服务套接字等待对方的连接和文件发送
ServerSocketserSocket;
//由服务套接字产生的套接字
SockettempSocket;
//用于读取
InputStreaminSocket;
//随机访问文件
RandomAccessFileinFile=null;
//临时缓寸区
bytebyteBuffer[]=newbyte[1024];
//默认用10000端口监听请求
intdefaultBindPort=10000;
//初始的绑定端口次数为0
inttryBindTimes=0;
//当前绑定的端口号是10000默认端口
intcurrentBindPort=defaultBindPort+tryBindTimes;/**
*@构造方法
*@抛出异常的原因是无法绑定服务的端口
**/
publicGetFile(intport)throwsException{
try{
//绑定服务的端口
this.bindToServerPort();}catch(Exceptione){
e.printStackTrace();
//绑定不成功重试
System.out.println(e.toString());
thrownewException("绑定端口不成功!
");}
//文件选择器以当前的目录打开
JFileChooserjfc=newJFileChooser(".");
jfc.showSaveDialog(newjav