?? clientacceptfile.java~58~
字號:
package chat;
import java.io.*;
import java.net.*;
import java.util.*;
/**
*
* <p>Title:用戶接收文件線程 </p>
* <p>Description: 用戶從套接中取得流并且保存到相應的文件中</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ClientAcceptFile implements Runnable {
Constant constant;
Socket acceptFileSocket=null;//接收該文件的套接
DataInputStream dis=null;//接收該文件的輸入流
DataOutputStream dos=null;//接收該文件的輸出流
RandomAccessFile raf=null;//保存文件的randomaccessfile對象
FileOutputStream fos;
File file=null;//新的保存文件對象
String key=null;
String ip=null;
String action=null;
String oldFileName=null;//舊的文件對象(從套接中取得)
String requestPeople;
String userName;
long readLength;
long fileLength;
FileDisposeButton fileDisposeButton;
static Hashtable saveThread=new Hashtable();
/**
*
* @param requestPeople String 請求發送文件的對象
* @param file File 新選擇的要保存的文件對象
* @param ip String 請求者的IP地址
* @param port int 請求者的端口
* @param action String 動作標識
* @param oldFileName String 從請求者中取得的文件對象名
* @param fileLength String 文件大小
*/
public ClientAcceptFile(String requestPeople,File file,
String ip,int port,String action,String oldFileName,String fileLength) {
constant=new Constant();
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);//保存當前線程到 hashtable中
//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 + constant.getLang("GG_xnfsdfj") +
this.file.getName() +constant.getLang("GG_zzfsz"));//設置相應的提示信息
while ( (readBuffer = dis.read(buffer)) != -1) {//從流中取得數據
raf.write(buffer, 0, readBuffer);//寫入文件中
readLength += readBuffer;
}
fileDisposeButton.buttonJPanel.setVisible(false);//文件保存完畢,按鈕對象為不可見
if (this.raf.length() == this.readLength) {//寫入的文件大小和原文件大小一樣
fileDisposeButton.fileInfo.setText(this.requestPeople + constant.getLang("GG_xnfsdfj") +
this.file.getName() + constant.getLang("GG_fswb")+constant.getLang("GG_wjbcz")+this.file.getAbsolutePath());
}else {
fileDisposeButton.fileInfo.setText(this.requestPeople + constant.getLang("GG_qxwj")+
this.file.getName() + constant.getLang("GG_dfs"));
}
}
else if (action.equals("DISAGREE")) {
dos.writeUTF("DISAGREE_ACCEPT:" + this.userName + "#TOSENDFILE#" +
this.oldFileName);
}
}
catch (IOException ioe) {
try {
fileDisposeButton.fileInfo.setText(this.requestPeople + constant.getLang("GG_xnfsdfj") +
this.file.getName() + constant.getLang("GG_yywlgzezz"));
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 + -