Java连接URL并下载zip但在提取zip时未正确下载

时间:2013-05-07 13:28:37

标签: java zip inputstream httpurlconnection outputstream

我正在向URL发送请求XML并接收到给定路径的zip文件。 有时我在这个zip文件的带宽很低时遇到麻烦,很可能120MB大小没有正确下载。并在解压缩zip文件时出错。从代码中提取也是如此。当我以高带宽下载时,这个文件可以毫无问题地下载。

我正在寻找一种解决方案而不会带宽高,从程序级别有没有办法下载这个zip文件,可能是部分或类似的东西?或者您所拥有的任何其他解决方案都非常感谢。

下载:

        url = new URL(_URL);
        sc = (HttpURLConnection) url.openConnection();
        sc.setDoInput(true);
        sc.setDoOutput(true);
        sc.setRequestMethod("POST");
        sc.connect();

        OutputStream mOstr = sc.getOutputStream();
        mOstr.write(request.getBytes());

        InputStream in = sc.getInputStream();

        FileOutputStream out = new FileOutputStream(path);

        int count;
        byte[] buffer = new byte[86384];
        while ((count = in.read(buffer,0,buffer.length)) > 0)
            out.write(buffer, 0, count);

        out.close();

摘录:

try {

        ZipFile zipFile = new ZipFile(path+zFile);

        Enumeration<?> enu = zipFile.entries();

        while (enu.hasMoreElements()) {

            ZipEntry zipEntry = (ZipEntry) enu.nextElement();

            String name = path+"/data_FILES/"+zipEntry.getName();
            long size = zipEntry.getSize();
            long compressedSize = zipEntry.getCompressedSize();
            System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", name, size, compressedSize);

            File file = new File(name);

            if (name.endsWith("/")) {
                file.mkdirs();
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);

            FileOutputStream fos = new FileOutputStream(file);

            byte[] bytes = new byte[86384];
            int length;

            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();

    } catch (Exception e) {
        log("Error in extracting zip file ");
        e.printStackTrace();
    }

0 个答案:

没有答案