?? filesendthread.java
字號:
package XXRoom;
import java.net.*;
import java.io.*;
//發送文件的線程, 當文件接收完畢發送一個標志位
class FileSendThread extends Thread implements FileThread
{
Client client;
ServerSocket sSkt;
String filePath;
FileInputStream fis;
Socket skt;
OutputStream write;
String name;//要發送的用戶的名字
public FileSendThread ( Client client , String filePath , String name )
{
this.client = client;
this.filePath = filePath;
this.name = name;
client.isFileTrans = true;
client.file.setEnabled( false );
client.cancel.setEnabled( true );
client.fileCondition.setText( "等待接收..." );
int i = 1000;
//尋找可用端口
while( true ) {
try
{
sSkt = new ServerSocket( i );
break;
}
catch ( IOException e )
{
i++;
continue;
}
}
}
public void run()
{
try
{
skt = sSkt.accept();
write = skt.getOutputStream();
}
catch ( IOException e )
{
client.appendPersonalSystemMsg( "連接發生網絡錯誤" );
dispose();
return;
}
//傳送文件
try
{
client.fileCondition.setText( "正在傳送文件 " + new File( filePath ).getName() );
fis = new FileInputStream( filePath );
byte buffer[] = new byte[ 1024 ];
while( fis.read( buffer ) != -1 ) {
write.write( buffer );
}
write.close();
fis.close();
skt.close();
sSkt.close();
}
catch ( FileNotFoundException e )
{
client.appendPersonalSystemMsg ( "找不到要傳送的文件" + filePath );
dispose();
return;
}
catch ( IOException e )
{
client.appendPersonalSystemMsg ( "傳送文件發生IO異常或對方取消了文件傳送" );
dispose();
return;
}
// JOptionPane.showMessageDialog( client, "文件發送完畢" );
dispose();
}
public void dispose() {
client.isFileTrans = false;
client.file.setEnabled( true );
client.cancel.setEnabled( false );
client.fileCondition.setText( "" );
try
{
if( write != null )
write.close();
if( fis != null )
fis.close();
if( skt != null )
skt.close();
if( sSkt != null )
sSkt.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -