📄 clientacceptfile.java~114~
字号:
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);
//System.out.println("AGREE_ACCEPT:" + this.requestPeople +
//"#TOSENDFILE#" + this.oldFileName);
fileDisposeButton=(FileDisposeButton)FileDisposeButton.saveFileDisposeButton.get(this.requestPeople+this.oldFileName);
//FileDisposeButton.fileInfo.setText(this.requestPeople+"向您发送的文件["+this.file.getName()+"]正在发送中...");
//fileDisposeButton.saveElseButton.setVisible(false);
//fileDisposeButton.disagreeButton.setVisible(false);
//fileDisposeButton.agreeButton.setText("取消");
//System.out.println(fileDisposeButton.fileInfo.getText());
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() + "]发送完毕.");
}else{
fileDisposeButton.fileInfo.setText(this.requestPeople + "取消文件[" +
this.file.getName() + "]的发送.");
}
// System.out.println("this.file.length="+this.file.length()+" this.filelength"+this.fileLength);
//System.out.println("接受完毕");
}else if(action.equals("DISAGREE")){
dos.writeUTF("DISAGREE_ACCEPT:" + this.userName + "#TOSENDFILE#" +
this.oldFileName);
}
//this.dis.close();
// this.dos.flush();
// this.dos.close();
//this.raf.close();
// this.acceptFileSocket.close();
}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 + -