?? clientacceptfile.java~120~
字號:
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientAcceptFile implements Runnable {
Socket acceptFileSocket=null;
DataInputStream dis=null;
DataOutputStream dos=null;
RandomAccessFile raf=null;
FileOutputStream fos;
File file=null;
String key=null;
String ip=null;
String action=null;
String oldFileName=null;
String requestPeople;
String userName;
long fileLength;
FileDisposeButton fileDisposeButton;
static Hashtable saveThread=new Hashtable();
public ClientAcceptFile(String requestPeople,File file,
String ip,int port,String action,String oldFileName,String fileLength) {
this.file=file;
this.ip=ip.substring(1);
this.requestPeople=requestPeople;
this.action=action;
this.oldFileName=oldFileName;
this.fileLength=(Long.valueOf(fileLength)).longValue();
userName=ChatArea.loginDialog.getName();
System.out.println(this.fileLength);
key=new String(requestPeople+this.oldFileName);
saveThread.put(key,this);
try{
acceptFileSocket = new Socket(this.ip, port);
}catch(IOException e){
e.printStackTrace();
}
}
public void run() {
byte[] buffer = new byte[8291];
int readBuffer = 0;
saveThread.put(key, this);
//System.out.println(key+"接受文件線程");
try {
dis = new DataInputStream(acceptFileSocket.getInputStream());
dos = new DataOutputStream(acceptFileSocket.getOutputStream());
raf = new RandomAccessFile(this.file, "rw");
//fos=new FileOutputStream(this.file);
if (action.equals("AGREE")) {
dos.writeUTF("AGREE_ACCEPT:" + this.userName + "#TOSENDFILE#" +
this.oldFileName);
fileDisposeButton = (FileDisposeButton) FileDisposeButton.
saveFileDisposeButton.get(this.requestPeople + this.oldFileName);
fileDisposeButton.fileInfo.setText(this.requestPeople + "向您發送的文件[" +
this.file.getName() + "]正在發送中...");
while ( (readBuffer = dis.read(buffer)) != -1) {
raf.write(buffer, 0, readBuffer);
}
fileDisposeButton.buttonJPanel.setVisible(false);
if (this.raf.length() == this.fileLength) {
fileDisposeButton.fileInfo.setText(this.requestPeople + "向您發送的文件[" +
this.file.getName() + "]發送完畢."+"文件保存在"+this.file.getAbsolutePath());
}
else {
fileDisposeButton.fileInfo.setText(this.requestPeople + "取消文件[" +
this.file.getName() + "]的發送.");
}
}
else if (action.equals("DISAGREE")) {
dos.writeUTF("DISAGREE_ACCEPT:" + this.userName + "#TOSENDFILE#" +
this.oldFileName);
}
}
catch (IOException ioe) {
try {
fileDisposeButton.fileInfo.setText(this.requestPeople + "向您發送的文件[" +
this.file.getName() + "]由于網絡故障而終止.");
this.dis.close();
this.dos.flush();
this.dos.close();
this.raf.close();
this.acceptFileSocket.close();
saveThread.remove(this);
}
catch (Exception e) {
}
}
finally {
try {
this.dis.close();
this.dos.flush();
this.dos.close();
this.raf.close();
this.acceptFileSocket.close();
saveThread.remove(this);
//System.out.println("jieshowenjianexcpeiont");
}
catch (IOException e) {
//e.printStackTrace();
}
}
}
public static void main(String[] args) {
// File f=new File()
//Thread thread=new Thread(new ClientAcceptFile("","","",0));
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -