Client-Server發送文件

客戶端向服務器端發送文件,客戶端發送按鈕,服務器端處於開啓狀態。

客戶端:send函數+發送文件按鈕事件處理;服務器端:recieve函數+開啓的一條線程

客戶端

//傳輸文件函數
public void send() {
byte[] sendBytes = null;
Socket socket = null;
DataOutputStream dos = null;
DataInputStream dis = null;
FileInputStream fis = null;
String s = filePathFiled.getText();//要傳輸的文件路徑
boolean bool = false;
try {
File file = new File(s); //根據路徑創建File對象
fixedL = file.length();//以字節爲單位的文件長度
socket = new Socket();
socket.connect(new InetSocketAddress(this.IP, 8888));//8888端口,this.IP在構造函數中已被初始化爲服務器IP
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
fis = new FileInputStream(file);
sendBytes = new byte[1024 * 10];
String type = s.substring(s.lastIndexOf("."));
dos.writeUTF(this.stuNo + "_" + this.stuName + type);// 發送部分1:爲了創建一個文件對象,並命名完善
progressBar.setMaximum(100);
while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
sumL += length;
progressBar.setValue((int) ((sumL / fixedL) * 100));//進度條設置進度、步步更新
dos.write(sendBytes, 0, length);//發送部分2:文件內容
dos.flush();
}
if (sumL == fixedL) {// 雖然數據類型不同,但JAVA會自動轉換成相同數據類型後在做比較
bool = true;
sumL = fixedL = 0;//若要限定只能交一次,就是加上語句sendButton.setEnabled(false);
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(contentPane, "系統找不到指定的文件!");
bool = false;
} finally {
try {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (fis != null)
fis.close();
if (socket != null)
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(contentPane, bool ? "上傳成功!" : "上傳失敗!");
}

//事件處理

//發送文件及其監聽器處理
sendButton = new JButton("\u53D1\u9001");
sendButton.addActionListener(new ActionListener() {//匿名內部類
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
send();
}//線程運行的目標類
}).start();//點擊按鈕啓動傳輸線程,線程處於就緒態;運行則取決於操作系統
}
});
sendButton.setBounds(389, 34, 93, 23);//(x,y,寬,高)
contentPane.add(sendButton);

服務器端

//接收客戶端上傳的文件
public void recieve(Socket socket) throws IOException {
byte[] inputByte = null;
int length = 0;
DataInputStream dis = new DataInputStream(socket.getInputStream());
FileOutputStream fos = null;
String info = dis.readUTF();// 讀取文件名字符串
String filePath = savePath.getText() + "/" +info;//存儲文件的路徑及文件名稱
try {
File f = new File(this.savePath.getText());
if (!f.exists()) {
f.mkdir();
}
fos = new FileOutputStream(new File(filePath));
inputByte = new byte[1024];
while ((length = dis.read(inputByte, 0, inputByte.length)) != -1) {
fos.write(inputByte, 0, length);
fos.flush();
}
items.addElement(info);//接收成功一次,列表就會添加一項
}catch (Exception e) {
e.printStackTrace();
}finally {
if (fos != null)
fos.close();
if (dis != null)
dis.close();
if (socket != null)
socket.close();
}
}

 /**
* 構造時,啓動兩條線程,8888端口接收文件、9527消息傳遞收錄服務器端產生的Socket對象,不同端口不同服務
* while循環一定要有,實現服務器開啓時任何時候客戶端連接服務器均可
* 又因爲while中的accept語句具有阻塞功能,所以放到線程中。
*/
new Thread(new Runnable() {//這條線程用來循環
public void run() {//在循環中,每連接一個客戶端就創建一個線程進行文件接收,達到同時上傳的目的
try {
@SuppressWarnings("resource")
ServerSocket serverFile = new ServerSocket(8888);
while (true) {
final Socket socket = serverFile.accept();
new Thread(new Runnable() {
public void run() {
try {
recieve(socket);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}).start();

以上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章