用JAVA SOCKET发送和接收文件Word格式文档下载.docx
《用JAVA SOCKET发送和接收文件Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《用JAVA SOCKET发送和接收文件Word格式文档下载.docx(13页珍藏版)》请在冰豆网上搜索。
*并告诉客户端服务的端口号以使其能正确连接到该端口上;
*@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(){
if(this.inFile==null){
System.out.println("
没有选择文件"
//关闭服务方套接字
this.serSocket.close();
//没有选择文件
return;
waitfor..."
+'
\n'
+"
等待对方接入"
//等待对方的连接
tempSocket=serSocket.accept();
//五秒钟连不上将抛出异常
this.serSocket.setSoTimeout(5000);
//获取输入流
this.inSocket=tempSocket.getInputStream();
}catch(Exceptionex){
System.out.println(ex.toString());
ex.printStackTrace();
//以下为传送文件代码和套接字清理工作
intamount;
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);
GetOK"
接收完毕!
//关闭文件
inFile.close();
//关闭临时套接字
tempSocket.close();
}catch(IOExceptione){
*@绑定端口
*@throwsException抛出异常的原因是无法绑定服务的端口
privatevoidbindToServerPort()throwsException{
//输出绑定的端口号到当前的控制台上
试绑定的端口号是:
+this.currentBindPort);
//在自己的机器上开一个服务类套接字并等待发送者的连接
serSocket=newServerSocket(this.currentBindPort);
//试了不止一次了
this.tryBindTimes=this.tryBindTimes+1;
//可查看试的次数getTryBindedTimes
this.currentBindPort=this.defaultBindPort+this.tryBindTimes;
//如果试的次数超过20次退出
if(this.tryBindTimes>
=20){
无法绑定到指定端口"
试了太多次了!
//递归的绑定
成功绑定的端口号是:
"
//获取试绑定的端口
publicintgetTryBindedTimes(){
returnthis.tryBindTimes;
//获取已经绑定的端口
publicintgetCurrentBindingPort(){
returnthis.currentBindPort;
*@测试方法
*@paramargs
publicstaticvoidmain(Stringargs[]){
GetFilegetFile=null;
getFile=newGetFile(10000);
无法传送文件!
System.exit
(1);
getFile.start();
}/*服务器端接收文件*/
/**
*@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