2012-12-24 47 views
0

現在它在終端之間發送圖像。客戶端將圖片翻譯成字節流,然後分發給服務器。服務器發現發送人並將數據發送到終端。現在在局域網的兩站模擬器中發送大圖。 發送和接收沒有問題。部署到網絡服務器,服務器接收代碼。Android套接字發送大圖像並丟失數據

int length = 0; 
int totalNum = 0; 
byte[] buffer = new byte[1024]; 
while ((length = dis.readInt()) != 0) { 
    length = dis.read(buffer, 0, length); 
    System.out.println("length :-------->" + length); 

    totalNum += length; 
    out.writeInt(length); 
    out.write(buffer, 0, length); 
    out.flush(); 
} 
System.out.println("totalNum:-------->" + totalNum); 
out.writeInt(0); 
out.flush(); 
Debug.info("totalNum::::" + totalNum); 
initService.getEnterpriseMsgService().save(msg); 

它每次收到1024字節。有時到System.out.println("length :-------->" + length);它是空的。仿真器發送和接收數據每次都是一致的。當我發送大圖片時,它沒有問題。我不知道它是關於代碼還是服務器問題。

尋求解決方案。提前致謝。

回答

1

在發送和接收image.You必須增加緩衝區大小的情況下

byte[] buffer = new byte[4096]; 
0

爲什麼不試試這樣:

InputStream is //your InputStream 
OutputStream out //your OutputStream 
byte[] buffer = new byte[1024]; 
int length = 0; 
try { 
    while ((length = is.read(buffer)) > 0) { 
     out.write(buffer, 0, length); 
    } 

} catch (Exception e) { 
    // TODO: handle exception 
}