package com.neu.nsr.cloudsecurity.demoapp.client;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JTextArea;
public class SendFileThread extends Thread {
private DataOutputStream dos = null;
private DataInputStream dis = null;
private BufferedReader bufferedReader = null;
private Socket socket = null;
private int length;
private String line = null;
private JTextArea textArea;
private File file;
private int intervalTime = 5; // 发送间隔(秒)
private boolean runFlag = false;
private String destIp;
private int destPort;
public SendFileThread(File file, String destIp, int destPort, JTextArea textArea) {
this.textArea = textArea;
this.file = file;
this.destIp = destIp;
this.destPort = destPort;
}
@Override
public void run() {
while (true && this.isInterrupted()==false) {
if (this.runFlag) {
sendFile();
try {
Thread.sleep(this.intervalTime * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void sendFile() {
try {
this.appendShowInfo("向" + destIp + ":" + destPort + "发送文件:" + file + ", 文件内容:");
this.appendShowInfo(this.file);
this.socket = new Socket(destIp, destPort);
this.socket.setSoTimeout(3000);
// 与客户端建立通信,获取输入流,读取取客户端提供的信息
dos = new DataOutputStream(socket.getOutputStream()); // socket数据输出流
dis = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); // socket数据输入流
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
// 缓冲区
byte[] bufArray = new byte[1024];
dos.writeUTF(file.getName());
dos.writeLong(file.length());
// 发送文件
while((length = dis.read(bufArray, 0, bufArray.length)) > 0){
dos.write(bufArray, 0, length);
dos.flush();
}
dos.flush();
// 接受文件接收端回执信息
try {
while ((line = bufferedReader.readLine()) != null) {
this.appendShowInfo("文件接受端消息回执: " + line);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("time out");
}
this.appendShowInfo("");
} catch (ConnectException e) {
e.printStackTrace();
this.appendShowInfo("连接失败!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null)
bufferedReader.close();
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean isRunFlag() {
return runFlag;
}
public void setRunFlag(boolean runFlag) {
this.runFlag = runFlag;
}
public int getIntervalTime() {
return intervalTime;
}
public void setIntervalTime(int intervalTime) {
this.intervalTime = intervalTime;
}
private void appendShowInfo(String info) {
this.textArea.setText(this.textArea.getText() + info + "\r\n");
this.textArea.selectAll();
this.textArea.setCaretPosition(this.textArea.getSelectedText().length());
this.textArea.requestFocus();
}
private void appendShowInfo(File file) {
String line;
BufferedReader bf;
try {
bf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while((line = bf.readLine()) != null){
appendShowInfo(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
appendShowInfo("文件不存在");
} catch (Exception e) {
e.printStackTrace();
}
}
}