采用報(bào)頭形式發(fā)送文件给僵,發(fā)送的整塊字節(jié)由4部分組成瘪菌。
- 文件的名稱轉(zhuǎn)成字節(jié)后,計(jì)算該字節(jié)長(zhǎng)度紧卒;
- 文件名轉(zhuǎn)成的字節(jié) ;
- 把文件轉(zhuǎn)成字節(jié),計(jì)算該文件字節(jié)的長(zhǎng)度;
- 文件轉(zhuǎn)的字節(jié)
服務(wù)端通過讀取長(zhǎng)度知道后面該長(zhǎng)度的字節(jié)為文件名或文件诗祸。
文件名長(zhǎng)度用4個(gè)字節(jié)裝載 | 文件名長(zhǎng)度不限 | 文件長(zhǎng)度用4個(gè)字節(jié)裝載 | 文件長(zhǎng)度不限 |
---|---|---|---|
A1 | A2 | A3 | A4 |
01110111... | 111000000... | 01110111... | 0101010100100001110101001001010010101010... |
在字節(jié)流中跑芳,A1占體積4個(gè)字節(jié)轴总,A1表示A2的長(zhǎng)度,A3占體積4個(gè)字節(jié)博个,A3表示A4的長(zhǎng)度
- 第一步怀樟,建立連接
private ExecutorService executor = Executors.newFixedThreadPool(5);
.
.
.
Socket socket = new Socket(host, Integer.parseInt(port));
executor.submit(new SendMusicRunnable(socket, musicEntity));
- 第二步,建立實(shí)現(xiàn)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();//文件在手機(jī)里的位置
String fileName= musicEntity.getFileName();//需要傳遞后綴盆佣,不能使用getTitle
//準(zhǔn)備標(biāo)題和標(biāo)題字節(jié)長(zhǎng)度
//標(biāo)題內(nèi)容
byte[] titleContentBytes = fileName.getBytes(Charset.defaultCharset());
int titleLength = titleContentBytes.length;
//標(biāo)題轉(zhuǎn)字節(jié)后的長(zhǎng)度往堡,用4字節(jié)裝標(biāo)題內(nèi)容轉(zhuǎn)字節(jié)后的長(zhǎng)度
byte[] titleLengthBytes = intToByteArray(titleLength);
//準(zhǔn)備文件長(zhǎng)度
File file = new File(url);
long musicLength = file.length();//文件屬性數(shù)組
//文件長(zhǎng)度轉(zhuǎn)字節(jié)后的長(zhǎng)度,4字節(jié)裝文件大小轉(zhuǎn)字節(jié)后的長(zhǎng)度
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.寫入標(biāo)題的字節(jié)長(zhǎng)度
dos.write(titleContentBytes);//2.寫入標(biāo)題
dos.write(fileLengthBytes);//3.寫入文件的字節(jié)長(zhǎng)度
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.傳遞結(jié)束
Log.e("tag", "【SendMusicRunnable】類的方法:【run】: " + "傳文件結(jié)束");
} catch (IOException e) {
//傳輸失敗
e.printStackTrace();
}finally{
if(dis!=null){
dis.close();
}
if(dos!=null){
dos.close();
}
}
}
}
/**
* int類型轉(zhuǎn)成4個(gè)字節(jié)的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;
}
至此客戶端傳輸結(jié)束。
C#寫的服務(wù)端執(zhí)行流程大概如下
- 開始接收字節(jié)流共耍;
- 判斷接收了4個(gè)字節(jié)投蝉,然后知道接下來的n個(gè)字節(jié)是文件名稱;
- 讀取文件名稱結(jié)束后征堪,即讀到第4+n字節(jié),再截獲第(4+n)~(4+n+4)字節(jié)关拒;
- 知道文件長(zhǎng)度為xn佃蚜;
- 然后一直讀到結(jié)束,保存文件到指定地址着绊,判斷文件長(zhǎng)度和xn是否相等谐算,不相等刪除文件。
下次再更新C#寫服務(wù)端的代碼归露。