?? clientsendfilethread.java~48~
字號:
import java.net.*;
import java.io.*;
import java.util.*;
import java.io.RandomAccessFile;
public class ClientSendFileThread extends Thread{
File file=null;
String toPeople=null;
Socket socket=null;
DataInputStream dis;
DataOutputStream dos;
FileInputStream fis;
RandomAccessFile raf;
BufferedOutputStream bos;
long readLength;
static Hashtable saveThread=new Hashtable();
FileCancelButton fileCancelButton;
String key;
public ClientSendFileThread(File file,String toPeople,Socket socket){
this.file=file;
this.toPeople=toPeople;
this.socket=socket;
this.key=toPeople+file;
System.out.println(this.file+" "+this.toPeople);
}
public void run(){
saveThread.put(key,this);
//System.out.println(key+"clientsendfilethread");
byte[] buffer=new byte[8291];
int readByte=0;
try{
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
//bos=new BufferedOutputStream(dos);
fis=new FileInputStream(file);
raf=new RandomAccessFile(file,"r");
}catch(IOException e){
e.printStackTrace();
}
try{
//while((readByte=fis.read(buffer))!=-1){
fileCancelButton=(FileCancelButton)FileCancelButton.saveFileCancel.get(this.key);
fileCancelButton.fileInfo.setText("您向"+this.toPeople+"發送的文件"+this.file+"正在傳送中...");
fileCancelButton.cancelSendButton.setVisible(true);
while((readByte=raf.read(buffer))!=-1){
dos.write(buffer,0,readByte);
this.readLength+=readByte;
//bos.write(buffer);
}
//this.dos.flush();
//this.dos.close();
//this.dis.close();
//this.raf.close();
//this.socket.close();
if(this.file.length()==this.readLength){
fileCancelButton.fileInfo.setText("您向"+this.toPeople+"發送的文件["+this.file.getName()+"]發送完畢");
fileCancelButton.buttonJPanel.setVisible(false);
}else{
fileCancelButton.fileInfo.setText(this.toPeople+"取消了文件["+this.file.getName()+"]的發送");
}
System.out.println("讀取完畢!");
}catch(IOException ioe){
try{
fileCancelButton.fileInfo.setText(this.toPeople+"取消文件["+this.file.getName()+"]的發送");
fileCancelButton.buttonJPanel.setVisible(false);
this.dos.flush();
this.dos.close();
this.dis.close();
this.raf.close();
this.socket.close();
this.saveThread.remove(this);
}catch(Exception e){
}
}finally{
try{
this.dos.flush();
this.dos.close();
this.dis.close();
this.raf.close();
this.socket.close();
}catch(Exception e){
}
}
}
public static void main(String[] args) {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -