【发布时间】:2024-01-16 13:04:01
【问题描述】:
我将MultiPart 内容发送到我的远程服务器以将其存储在文件系统中。为此,我使用 Java TCP/IP 协议。为了避免网络带宽和 TCP 输入/输出缓冲区内存,我以GZIP 压缩格式发送数据。但是,我无法解压缩从客户端收到的数据。我得到了 Unexpected end of ZLIB input stream 异常。这是由于服务器正在接收 chunks 中的数据。
Java 代码
客户
OutputStream out = new GZIPOutputStream(sock.getOutputStream());
byte[] dataToSend = FileUtil.readFile(new File("/Users/bharathi/Downloads/programming_in_go.pdf"));
out.write(dataToSend);
服务器
out = new FileOutputStream("/Users/bharathi/Documents/request_trace.log");
InputStream in = new GZIPInputStream(clntSocket.getInputStream());
int totalBytesRead = 0;
int bytesRead;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = in.read(buffer)) != -1)
{
out.write(buffer , 0 , bytesRead);
totalBytesRead += bytesRead;
}
有什么办法可以在Socket中发送GZIP压缩格式的数据吗?
【问题讨论】:
-
您可能希望更改协议以首先发送要读取的大小。