使用Java下载后ZipFile已损坏
使用Java下载的Zip文件,打开时表示无法打开。 想知道什么是pblm吗? 是因为内存不足吗 下面是下载zipFiles的代码使用Java下载后ZipFile已损坏,java,applet,Java,Applet,使用Java下载的Zip文件,打开时表示无法打开。 想知道什么是pblm吗? 是因为内存不足吗 下面是下载zipFiles的代码 try { for(int i=0;i<URL_LOCATION.length;i++) { url = new URL(URL_LOCATION[i]); connection = url.openConnection(); stream = new BufferedInputStream(connec
try {
for(int i=0;i<URL_LOCATION.length;i++) {
url = new URL(URL_LOCATION[i]);
connection = url.openConnection();
stream = new BufferedInputStream(connection.getInputStream());
int available = stream.available();
b = new byte[available];
stream.read(b);
File file = new File(LOCAL_FILE[i]);
OutputStream out = new FileOutputStream(file);
out.write(b);
}
} catch (Exception e) {
System.err.println(e.toString());
}
您正在使用available()-调用来确定要读取的字节数。这显然是错误的(有关详细信息,请参阅InputStream的javadoc)。available()只告诉您立即可用的数据,而不是真正的流长度 您需要一个循环并从流中读取,直到它返回-1(对于EndOfStream)作为读取的字节数
我建议您阅读有关streams的教程:您使用java创建zipfile?共享代码。是的,使用了zipFile,还尝试了ZipInputStream。我已经在wampserver中使用了zipFile。要使用zipFile下载该文件。请添加一些代码,带有堆栈跟踪。下载一切正常,但无法打开下载的文件。请修改您的问题以包含代码。没有尝试过。不工作。为什么下载的zip文件已损坏?不能打开吗?
BufferedInputStream in = null;
FileOutputStream fout = null;
try
{
in = new BufferedInputStream(new URL(urlString).openStream());
fout = new FileOutputStream(filename);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1)
{
fout.write(data, 0, count);
}
}
finally
{
if (in != null)
in.close();
if (fout != null)
fout.close();
}