這是一個簡單的包含發送端和接收端的例子。發送端向接收端傳送檔案名和檔案內容,接收端將收到的檔案儲存在磁碟上。接收端可以同時接收多個發送端傳來的檔案,但沒有處理檔案同名的情況。
這個例子中設計了一個簡單的協議。發送的內容是這樣的:
檔案名稱長度(4位元組)—檔案名稱—檔案內容長度(4位元組)—檔案內容。
接收端也按照這個結構進行解析。建議先看 Client 類,再看 Server 類。
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 簡單的檔案發送與接收樣本
*/
public class FileTrasmission {
//程式入口
public static void main(String[] args) throws Exception {
int port = 7788;
new Server(port, "c:\\save\\").start();
new Client().sendFile("127.0.0.1", port, "c:\\迷失在康熙末年.txt");
}
}
/**
* 接收端。可同時接收多個發送端發來的檔案。但如果發來的檔案是同名的話那就亂了。
*/
class Server {
private int listenPort;
private String savePath;
/**
* 構造方法
*
* @param listenPort 偵聽連接埠
* @param savePath 接收的檔案要儲存的路徑
*
* @throws IOException 如果建立儲存路徑失敗
*/
Server(int listenPort, String savePath) throws IOException {
this.listenPort = listenPort;
this.savePath = savePath;
File file = new File(savePath);
if (!file.exists() && !file.mkdirs()) {
throw new IOException("無法建立檔案夾 " + savePath);
}
}
// 開始偵聽
public void start() {
new ListenThread().start();
}
// 網上抄來的,將位元組轉成 int。b 長度不得小於 4,且只會取前 4 位。
public static int b2i(byte[] b) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (b[i] & 0x000000FF) << shift;
}
return value;
}
/**
* 偵聽線程
*/
private class ListenThread extends Thread {
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(listenPort);
// 開始迴圈
while (true) {
Socket socket = server.accept();
new HandleThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 讀取流並儲存檔案的線程
*/
private class HandleThread extends Thread {
private Socket socket;
private HandleThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
InputStream is = socket.getInputStream();
readAndSave(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// nothing to do
}
}
}
// 從流中讀取內容並儲存
private void readAndSave(InputStream is) throws IOException {
String filename = getFileName(is);
int file_len = readInteger(is);
System.out.println("接收檔案:" + filename + ",長度:" + file_len);
readAndSave0(is, savePath + filename, file_len);
System.out.println("檔案儲存成功(" + file_len + "位元組)。");
}
private void readAndSave0(InputStream is, String path, int file_len) throws IOException {
FileOutputStream os = getFileOS(path);
readAndWrite(is, os, file_len);
os.close();
}
// 邊讀邊寫,直到讀取 size 個位元組
private void readAndWrite(InputStream is, FileOutputStream os, int size) throws IOException {
byte[] buffer = new byte[4096];
int count = 0;
while (count < size) {
int n = is.read(buffer);
// 這裡沒有考慮 n = -1 的情況
os.write(buffer, 0, n);
count += n;
}
}
// 讀取檔案名稱
private String getFileName(InputStream is) throws IOException {
int name_len = readInteger(is);
byte[] result = new byte[name_len];
is.read(result);
return new String(result);
}
// 讀取一個數字
private int readInteger(InputStream is) throws IOException {
byte[] bytes = new byte[4];
is.read(bytes);
return b2i(bytes);
}
// 建立檔案並返回輸出資料流
private FileOutputStream getFileOS(String path) throws IOException {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
return new FileOutputStream(file);
}
}
}
======================================
/**
* 發送端
*/
class Client {
// 網上抄來的,將 int 轉成位元組
public static byte[] i2b(int i) {
return new byte[]{
(byte) ((i >> 24) & 0xFF),
(byte) ((i >> 16) & 0xFF),
(byte) ((i >> 8) & 0xFF),
(byte) (i & 0xFF)
};
}
/**
* 傳送檔案。檔案大小不能大於 {@link Integer#MAX_VALUE}
*
* @param hostname 接收端主機名稱或 IP 位址
* @param port 接收端連接埠號碼
* @param filepath 檔案路徑
*
* @throws IOException 如果讀取檔案或發送失敗
*/
public void sendFile(String hostname, int port, String filepath) throws IOException {
File file = new File(filepath);
FileInputStream is = new FileInputStream(filepath);
Socket socket = new Socket(hostname, port);
OutputStream os = socket.getOutputStream();
try {
int length = (int) file.length();
System.out.println("傳送檔案:" + file.getName() + ",長度:" + length);
// 傳送檔案名和檔案內容
writeFileName(file, os);
writeFileContent(is, os, length);
} finally {
os.close();
is.close();
}
}
// 輸出檔案內容
private void writeFileContent(InputStream is, OutputStream os, int length) throws IOException {
// 輸出檔案長度
os.write(i2b(length));
// 輸出檔案內容
byte[] buffer = new byte[4096];
int size;
while ((size = is.read(buffer)) != -1) {
os.write(buffer, 0, size);
}
}
// 輸出檔案名
private void writeFileName(File file, OutputStream os) throws IOException {
byte[] fn_bytes = file.getName().getBytes();
os.write(i2b(fn_bytes.length)); // 輸出檔案名長度
os.write(fn_bytes); // 輸出檔案名
}
}