?? filereceivethread.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 + -