I'm making a application in Java that download a file from a webserver. This file is a 816kb zip file. I've tested the application on 3 different computers and it isn't working for one computer. For that one it downloads only 13kb of the file and then stops. When I check the htaccess logs I see this:
我正在用Java编写一个从Web服务器下载文件的应用程序。此文件是一个816kb的zip文件。我已经在3台不同的计算机上测试了该应用程序,但它并不适用于一台计算机。对于那个,它只下载13kb的文件,然后停止。当我检查htaccess日志时,我看到了这个:
a: "GET /cache.zip HTTP/1.1" 200 816938 "-" "Mozilla/4.0 (Windows 7 6.1) Java/1.7.0_07"
a:“GET /cache.zip HTTP / 1.1”200 816938“ - ”“Mozilla / 4.0(Windows 7 6.1)Java / 1.7.0_07”
b: "GET /cache.zip HTTP/1.1" 200 134320 "-" "Mozilla/4.0 (Windows 7 6.1) Java/1.7.0_09"
b:“GET /cache.zip HTTP / 1.1”200 134320“ - ”“Mozilla / 4.0(Windows 7 6.1)Java / 1.7.0_09”
(PC a is working, PC b isn't working)
(PC a正在运行,PC b无法正常工作)
I've tried lots of different ways to download the file in java but for all ways it stops downloading after 13kb. I've also tried to run te application with 512m memory but that isn't the problem.
我已经尝试了很多不同的方法来下载java中的文件但是在13kb之后它会停止下载。我也尝试用512m内存运行te应用程序,但这不是问题。
This is what I have now:
这就是我现在拥有的:
DataInputStream in = new DataInputStream(conn.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(Config.CACHE_DIR+File.separator+"cache.zip")));
byte[] data = new byte[1024];
while((count = in.read(data,0,1024)) >= 0){
out.write(data, 0, count);
}
but this while loop won't stop so it gets stuck in in.read
但是这个while循环不会停止,所以它会卡在in.read中
2 个解决方案
#1
0
Run a simple standalone test, to make sure the problem is with the PC and not your software:
运行一个简单的独立测试,以确保问题出在PC而不是您的软件:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class Scratch {
public static void main(String[] args) throws IOException {
URL url = new URL("http://url.to.zip");
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream("test.zip");
byte[] buffer = new byte[1024];
int read = 0;
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
}
}
#2
0
I usually use Apache Commons IO IOUtils.copy() for copying between streams. It uses a buffer to copy bytes from one stream to another.
我通常使用Apache Commons IO IOUtils.copy()来在流之间进行复制。它使用缓冲区将字节从一个流复制到另一个流。
By the way, there's no need to use a DataInputStream and DataOutputStream wrappers in your case. You can just use the InputStream and FileOutputStream directly.
顺便说一句,在您的情况下,不需要使用DataInputStream和DataOutputStream包装器。您可以直接使用InputStream和FileOutputStream。
With IOUtils, the code will become:
使用IOUtils,代码将变为:
InputStream in = conn.getInputStream();
File outputFile = new File(Config.CACHE_DIR + File.separator + "cache.zip");
OutputStream out = new FileOutputStream(outputFile);
try {
IOUtils.copy(in, out);
} finally {
output.close();
}
If you don't want to use IOUtils...
如果你不想使用IOUtils ......
int count;
byte[] data = new byte[1024];
while ((count = in.read(data)) > 0) {
out.write(data, 0, count);
}
#1
0
Run a simple standalone test, to make sure the problem is with the PC and not your software:
运行一个简单的独立测试,以确保问题出在PC而不是您的软件:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class Scratch {
public static void main(String[] args) throws IOException {
URL url = new URL("http://url.to.zip");
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream("test.zip");
byte[] buffer = new byte[1024];
int read = 0;
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
}
}
#2
0
I usually use Apache Commons IO IOUtils.copy() for copying between streams. It uses a buffer to copy bytes from one stream to another.
我通常使用Apache Commons IO IOUtils.copy()来在流之间进行复制。它使用缓冲区将字节从一个流复制到另一个流。
By the way, there's no need to use a DataInputStream and DataOutputStream wrappers in your case. You can just use the InputStream and FileOutputStream directly.
顺便说一句,在您的情况下,不需要使用DataInputStream和DataOutputStream包装器。您可以直接使用InputStream和FileOutputStream。
With IOUtils, the code will become:
使用IOUtils,代码将变为:
InputStream in = conn.getInputStream();
File outputFile = new File(Config.CACHE_DIR + File.separator + "cache.zip");
OutputStream out = new FileOutputStream(outputFile);
try {
IOUtils.copy(in, out);
} finally {
output.close();
}
If you don't want to use IOUtils...
如果你不想使用IOUtils ......
int count;
byte[] data = new byte[1024];
while ((count = in.read(data)) > 0) {
out.write(data, 0, count);
}