📄 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 + -