Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编写下载zip文件的代码时发生I/O错误_Java - Fatal编程技术网

Java 编写下载zip文件的代码时发生I/O错误

Java 编写下载zip文件的代码时发生I/O错误,java,Java,我需要一个接一个地从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.

我需要一个接一个地从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\u SYSTEM\DPV102015\u 200.ZIP(文件名、目录名或卷标语法不正确)

既然您只想复制ZIP,为什么还要费心打开
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中的原始流。

您似乎试图打开一个名为URL的
ZipFile
ZipFile
仅适用于文件,而不适用于URL。此外,您似乎正在尝试从
ZipFile
读取条目,但从URL连接读取数据。它们之间没有关联,那么您认为这将如何工作?