http://blog.csdn.net/mhapdream/article/details/8929253 Socket:java與C之間的文件傳送<1>(C語言之間的通信)
http://blog.csdn.net/mhapdream/article/details/8929457 Socket:java與C之間的文件傳送<2>(JAVA語言之間的通信)
http://blog.csdn.net/mhapdream/article/details/8929590 Socket:java與C之間的文件傳送<3>(JAVA與C通信需要注意的地方)
http://blog.csdn.net/mhapdream/article/details/8929638 Socket:java與C之間的文件傳送<4>(JAVA與C通信)
其實絕大多數編程語言之間的通信原理是一樣的這邊文章來看一下java是如何進行文件傳輸的
建議先看 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); // 輸出文件名
}
}
看起來很複雜的樣子,對於一個剛剛接觸的人來說卻是不怎麼容易理解,那麼這是一個怎麼樣的邏輯的,我們分析一下就會變得比較簡單了;
首先來講這個例子是說一個完整的文件傳輸包括文件大小,文件名稱,文件內容。顯然對於我們來說要一個個得來。對於文件大小文件名稱,我們完全可以用一個固定的常量來代替,這樣就大大簡化了代碼的複雜度,最終也就變成了如何傳送文件內容!
1,讀取文件內容,
2,發送內容,
說起來好簡單的樣子但是要怎麼做呢?
FileInputStream is = new FileInputStream(this.FilePath);
byte Buffer[] = new byte[1024 * 8];
while ((size = is.read(buffer)) != -1) {
//System.out.println(size + " ");
outputstream.write(buffer, 0, size);
}
客戶端怎麼接收呢?
InputStream inputStream = socket.getInputStream();
int Size;
byte Buffer[] = new byte[1024 * 8];
while ((Size = inputStream.read(Buffer)) != -1) {
System.out.print(Size + " ");
system.out.print(new String11(buffer));//此處文檔顯示有問題所以加了個11,源碼需要去掉!
}
這樣看起來是不是就沒那麼複雜了呢 ,其實很多東西動手自己做,你會發現是如此的簡單!