采用報頭形式發送文件,發送的整塊字節由4部分組成。
- 文件的名稱轉成字節后,計算該字節長度;
- 文件名轉成的字節 ;
- 把文件轉成字節,計算該文件字節的長度;
- 文件轉的字節
服務端通過讀取長度知道后面該長度的字節為文件名或文件。
文件名長度用4個字節裝載 | 文件名長度不限 | 文件長度用4個字節裝載 | 文件長度不限 |
---|---|---|---|
A1 | A2 | A3 | A4 |
01110111... | 111000000... | 01110111... | 0101010100100001110101001001010010101010... |
在字節流中,A1占體積4個字節,A1表示A2的長度,A3占體積4個字節,A3表示A4的長度
- 第一步,建立連接
private ExecutorService executor = Executors.newFixedThreadPool(5);
.
.
.
Socket socket = new Socket(host, Integer.parseInt(port));
executor.submit(new SendMusicRunnable(socket, musicEntity));
- 第二步,建立實現Runnable接口的類
private class SendMusicRunnable implements Runnable {
private Socket socket;
private MusicEntity musicEntity;
SendMusicRunnable(Socket socket, MusicEntity musicEntity) {
this.socket = socket;
this.musicEntity = musicEntity;
}
@Override
public void run() {
String url = musicEntity.getUrl();//文件在手機里的位置
String fileName= musicEntity.getFileName();//需要傳遞后綴,不能使用getTitle
//準備標題和標題字節長度
//標題內容
byte[] titleContentBytes = fileName.getBytes(Charset.defaultCharset());
int titleLength = titleContentBytes.length;
//標題轉字節后的長度,用4字節裝標題內容轉字節后的長度
byte[] titleLengthBytes = intToByteArray(titleLength);
//準備文件長度
File file = new File(url);
long musicLength = file.length();//文件屬性數組
//文件長度轉字節后的長度,4字節裝文件大小轉字節后的長度
byte[] fileLengthBytes = intToByteArray((int) musicLength);
try {
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
DataOutputStream dos = new DataOutputStream(bos);
DataInputStream dis = new DataInputStream(bis);
//寫屬性
dos.write(titleLengthBytes);//1.寫入標題的字節長度
dos.write(titleContentBytes);//2.寫入標題
dos.write(fileLengthBytes);//3.寫入文件的字節長度
dos.flush();
int end = -1;
byte[] bytes = new byte[1024 * 1024];
int length = bytes.length;
long currentSize = 0;
//4.寫入文件
while ((end = dis.read(bytes, 0, length)) != -1) {
dos.write(bytes, 0, end);
}
dos.flush();
//5.傳遞結束
Log.e("tag", "【SendMusicRunnable】類的方法:【run】: " + "傳文件結束");
} catch (IOException e) {
//傳輸失敗
e.printStackTrace();
}finally{
if(dis!=null){
dis.close();
}
if(dos!=null){
dos.close();
}
}
}
}
/**
* int類型轉成4個字節的byte[]
**/
private static byte[] intToByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte) ((i >> 24) & 0xFF);
result[1] = (byte) ((i >> 16) & 0xFF);
result[2] = (byte) ((i >> 8) & 0xFF);
result[3] = (byte) (i & 0xFF);
return result;
}
至此客戶端傳輸結束。
C#寫的服務端執行流程大概如下
- 開始接收字節流;
- 判斷接收了4個字節,然后知道接下來的n個字節是文件名稱;
- 讀取文件名稱結束后,即讀到第4+n字節,再截獲第(4+n)~(4+n+4)字節;
- 知道文件長度為xn;
- 然后一直讀到結束,保存文件到指定地址,判斷文件長度和xn是否相等,不相等刪除文件。
下次再更新C#寫服務端的代碼。