繁体   English   中英

编写用于下载 zip 文件的代码时出现 I/O 错误

[英]I/O error while writing code for downloading a zip file

我需要从一个 URL 一个一个下载 zip 文件。 下面是我写的代码:

package Sample2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class MonthlyDB1 {


    public static void main(String[] args) throws IOException {
        URLConnection con = null;

        //int i;
            try {
                Authenticator.setDefault(new CustomAuthenticator());
                URL url = new URL("http://www.g1.com/Support/download.asp?fn=2LL\\ASCII\\2LL092015_200.zip&type=db&asset=1-JOKWT&dbAsset=1-JOKY2");
                ZipFile zipFile = new ZipFile("http://dl.g1.com/Release/databases/DPV/OPEN_SYSTEM/DPV102015_200.ZIP");
                File file= new File("Desktop\\DPV.zip");
                //BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())
                con = url.openConnection();
                BufferedInputStream bis = new BufferedInputStream( 
                        con.getInputStream()); 
                BufferedOutputStream bos = new BufferedOutputStream( 
                        new FileOutputStream(file.getName())); 
                Enumeration en = zipFile.entries(); 

                    while(en.hasMoreElements()){
                        ZipEntry entry = (ZipEntry)en.nextElement();
                        String entryName = entry.getName();
                        long compressedSize = entry.getCompressedSize();
                    byte[] b = new byte[(int) compressedSize];
                    int count;
                    while ((count = bis.read(b)) > 0) {
                    bos.write(b, 0, count);
                }}
                bos.flush(); 
                bis.close(); 
            }
            catch (MalformedURLException e) {
                System.out.println("Malformed URL: " + e.getMessage());
            }
            catch (IOException e) {
                System.out.println("I/O Error: " + e.getMessage()); 
            }
        }
        public static class CustomAuthenticator extends Authenticator {
            protected PasswordAuthentication getPasswordAuthentication() {
                String prompt = getRequestingPrompt();
                String hostname = getRequestingHost();
                InetAddress ipaddr = getRequestingSite();
                int port = getRequestingPort();
                String username = "stov1jypf6";
                String password = "1jypf6";
                // Return the information (a data holder that is used by Authenticator)
                return new PasswordAuthentication(username, password.toCharArray());
            }
        }

    }

但下面是我得到的错误;

I/O 错误:http:\\dl.g1.com\\Release\\databases\\DPV\\OPEN_SYSTEM\\DPV102015_200.ZIP(文件名、目录名或卷标语法不正确)

你为什么还要费心去打开一个Zip*Stream因为你只想复制 zip 文件?

下载 zip 的代码要简单得多:

final Path dst = Paths.get("Desktop\\DPV.zip");

final URL url = ...;

try (
    final InputStream in = url.openStream();
) {
    Files.copy(in, dst);
}

嗯,这是一个粗略的版本; 还有一个问题是您似乎没有在任何地方使用您的身份验证。 您可能打算使用支持身份验证的 HTTP 客户端,而不是来自 URL 的原始流。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM