⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filereceivethread.java

📁 java聊天室服务器
💻 JAVA
字号:
package XXRoom;

import java.net.*;
import java.io.*;

//接收文件的线程
class FileReceiveThread extends Thread implements FileThread
{
	Client client;
	Socket skt;
	String filePath;
	InputStream read;
	FileOutputStream fos;
	String name;//发送者的名字

	public FileReceiveThread ( Client client , String ip, int port , String filePath , String name ) throws UnknownHostException, 
		IOException
	{
		this.client = client;
		this.filePath = filePath;
		this.name = name;

		client.isFileTrans = true;
		client.file.setEnabled( false );
		client.cancel.setEnabled( true );

		skt = new Socket( ip, port );		
	}	
	

	public void run() 
	{
		client.fileCondition.setText( "正在接受文件 " + new File( filePath ).getName() );

		
		try
		{
			read = skt.getInputStream();			
		}
		catch ( IOException e )
		{
			client.appendPersonalSystemMsg ( "获取InputStream异常" );
			dispose();
			return;
		}
		
		try
		{
			fos = new FileOutputStream( filePath );
			byte buffer[] = new byte[ 1024 ];

			while( read.read( buffer ) != -1 ) {
				fos.write( buffer );
			}
			read.close();
			fos.close();
			skt.close();
		}
		catch ( FileNotFoundException e )
		{
			client.appendPersonalSystemMsg ( "文件传送异常" );
			dispose();
			return;
		}		
		catch ( IOException e ) 
		{
			client.appendPersonalSystemMsg ( "文件接收发生IO异常或对方取消了传送" );
			dispose();
			return;
		}		
	//	JOptionPane.showMessageDialog( client, "文件接收完毕" );
		client.appendPersonalSystemMsg( "文件接收完毕\n" );
		client.sendMessage( "SUCCESS " + name + " " + client.nickName );
		dispose();
	}

	public void dispose ()
	{
		client.isFileTrans = false;		
		client.cancel.setEnabled( false );
		client.fileCondition.setText( "" );

		try
		{
			if( skt != null )
				skt.close();
			if( read != null )
				read.close();
			if( fos != null )
				fos.close();
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -