[英]Socket Android with Java on WiFifreezing on sending file
我正在做一個帶有 Java 的服務器和一個帶有 Android 的客戶端。 一切正常,但是當我嘗試在 Android 上接收文件時,IO 輸入凍結。 我嘗試對 Java 測試應用程序執行相同的操作,並且工作正常。
我在客戶端的代碼:
int bytesRead;
long current = 0;
byte [] mybytearray = new byte[InfoPrograma.BUFFER_LENGTH];
//Receive file
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file)
);
while (
(bytesRead = is.read(
mybytearray,
0,
mybytearray.length
)
) >= 0) {
current += bytesRead;
bos.write(mybytearray, 0 , bytesRead);
}
bos.flush();
bos.close();
服務器端代碼:
File file = new File(fileStr);
byte[] buffer = new byte[InfoPrograma.BUFFER_LENGTH];
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file)
);
OutputStream os = socket.getOutputStream();
long tmp = 0;
while ((count = bis.read(buffer)) > 0) {
os.write(buffer, 0, count);
tmp += count;
System.out.println(tmp);
}
os.flush();
BUFFER_LENGHT 是 2000。如果您需要其他任何東西,請提出要求。
謝謝你。
完成了。 我發現的唯一方法是首先將文件的長度發送到客戶端,保存它,然后在(在我的代碼上)current < fileLength.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.