這真是令人尷尬。我想通過套接字發送一個大的JSON文件,並且字符串自然超出了限制,所以我試着壓縮它,但由於某種奇怪的原因,我只能在服務器端獲得單個字節。這是我到目前爲止有:通過套接字發送大型JSON文件
服務器端:
StringBuilder sb = new StringBuilder();
String str;
while((str = request.getReader().readLine()) != null)
sb.append(str);
Socket client = new Socket("localhost", 8081);
OutputStream outServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(new GZIPOutputStream(outServer));
byte[] buff = sb.toString().getBytes("UTF-8");
System.out.println(buff.length);
out.writeInt(buff.length);
out.write(buff);
out.flush();
client.close();
的BUF長度是在我試圖使這項工作的具體情況198153。
客戶端:
ServerSocket serverSocket = new ServerSocket(8081);
Socket server = serverSocket.accept();
DataInputStream in = new DataInputStream(new GZIPInputStream(server.getInputStream()));
System.out.println(in.available());
int len = in.readInt();
byte[] buff = new byte[len];
in.readFully(buff);
String response = new String(buff, "UTF-8");
System.out.println(response);
updateMessage(response);
server.close();
serverSocket.close();
的in.available()僅有1在這種情況下,程序不會再執行,它只是停在那裏(它不會終止雖然) 任何想法?我認爲這可能有助於獲得外部的觀點,因爲這可能是我錯過了某些東西,顯而易見的東西。謝謝。
甲側面說明:'InflaterInputStream.available()'將EOF後返回1 EOF之前和0。在這種情況下,它僅僅意味着流有數據。 –
「*字符串自然超出限制*」 - 您指的是什麼限制?爲什麼*客戶*創建*服務器套接字*,*服務器*創建*客戶套接字*?在任何情況下,與插座輸入打交道時,你通常應該包括輸入緩衝,如:'DataInputStream類中=新DataInputStream所(新GZIPInputStream(新的BufferedInputStream(server.getInputStream())));' –