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

时间:2015-11-14 11:57:58

标签: 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.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(文件名,目录名或卷标语法不正确)

1 个答案:

答案 0 :(得分:0)

为什么你甚至懒得尝试打开Zip*Stream,因为你要做的就是复制拉链?

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

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

final URL url = ...;

try (
    final InputStream in = url.openStream();
) {
    Files.copy(in, dst);
}
嗯,这是一个粗糙的版本;还有一个问题是您似乎没有在任何地方使用您的身份验证。您可能打算使用支持身份验证的HTTP客户端,而不是URL中的原始流。