我的Java服务器通过HTTP URL从Apache服务器读取Zip文件。该过程非常缓慢,读取整个文件大约需要6个小时。接近文件的末尾,它会抛出下面的异常。如果更快地读取文件,则不会发生异常。服务器之间没有防火墙。我该如何解决这个问题?
例外:
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:163)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:282)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:324)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:176)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:153)
at java.io.BufferedReader.readLine(BufferedReader.java:316)
at java.io.BufferedReader.readLine(BufferedReader.java:379)
JVM信息:
Java version: 1.6.0_34,Sun Microsystems Inc.
Java Runtime: OpenJDK Runtime Environment (build 1.6.0_34-b34)
Java VM: OpenJDK 64-Bit Server VM 23.25-b01,Sun Microsystems Inc.
OS-System: Linux 2.6.32-504.8.1.el6.x86_64,amd64
重现错误的Java代码:
public void process(long sleepMillis, String fileUrl) throws Exception {
BufferedReader buffReader = null;
ZipInputStream is = null;
try {
URL inputFileURL = new URL(fileUrl);
URLConnection con = inputFileURL.openConnection();
is = new ZipInputStream(con.getInputStream());
InputStreamReader reader = new InputStreamReader(is);
buffReader = new BufferedReader(reader);
ZipEntry ze;
while ((ze = is.getNextEntry()) != null) {
log("*** ZipEntry name:" + ze.getName() + ", size: "
+ ze.getSize());
int records = 0;
String buffer;
while ((buffer = buffReader.readLine()) != null) {
records++;
log("rec[" + records + "]" + buffer);
Thread.sleep(sleepMillis);
}
log("total recs: " + records);
}
} finally {
if (buffReader != null) {
buffReader.close();
}
if (is != null) {
is.close();
}
}
log("DONE");
}