使用HttpURLConnection下载文件 - 连接超时错误

时间:2017-11-07 10:23:38

标签: java httpurlconnection

我编写了一个java程序来从网址下载文件 - NSE Bhavcopy

但我得到了#34;线程'主要' java.net.ConnectException:连接超时:connect",我尝试了以下排列&组合,但没有运气。

  1. 使用代理&没有代理
  2. 使用身份验证&无
  3. 尝试了不同的网址
  4. 以下是代码&跟踪供参考,请有人帮助我

     String myUrl = "https://www.nseindia.com/content/historical/EQUITIES/2017/NOV/cm06NOV2017bhav.csv.zip";
     String fileName = "d:\\download.zip";
    
     Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.100.1.124", 3128));
    
     URL url = new URL(myUrl);
     HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
    
     String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
     String agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1";
    
     uc.setRequestProperty("Accept", accept);
     uc.setRequestProperty("User-Agent", agent);
     uc.setRequestMethod("GET");
     uc.setConnectTimeout(15*1000);
     Authenticator.setDefault(new MyAuthenticator());uc.connect();  //I get "connection timed out error" for this line
    

    微量

         Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:844)
    at cashflow.downloadMTM.option2(downloadMTM.java:76)
    at cashflow.downloadMTM.main(downloadMTM.java:71)
    

1 个答案:

答案 0 :(得分:0)

downloadFile(“http://www.bseindia.com/download/BhavCopy/Equity/EQ_ISINCODE_071117.zip”,“D:\ Market Feeds \ BSE Bhavcopy \”);

public static void downloadFile(String source, String destination) throws Exception { 

    try{
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.100.1.124", 3128));

    URL oracle = new URL(source);
    URLConnection yc = oracle.openConnection(proxy);

    InputStream in = yc.getInputStream();
    FileOutputStream out = new FileOutputStream(destination + "\\bhavcopy.zip");
    copySource2Dest(in, out, 1024);
    out.close();

    extractFolder(destination + "\\bhavcopy.zip", destination);

    Path path = FileSystems.getDefault().getPath(destination, "bhavcopy.zip");
    boolean succ = Files.deleteIfExists(path);

    System.out.println("Download is successfull");
    }
    catch(Exception e){
        System.out.println("Error in downloading : " + e);
    }
}

 public static void copySource2Dest(InputStream input, OutputStream output, int bufferSize)
         throws IOException {
             byte[] buf = new byte[bufferSize];
             int n = input.read(buf);
             while (n >= 0) {
                 output.write(buf, 0, n);
                 n = input.read(buf);
             }
             output.flush();
         }

public static void extractFolder(String zipFile,String extractFolder) 
{
    try
    {
        int BUFFER = 2048;
        File file = new File(zipFile);

        ZipFile zip = new ZipFile(file);
        String newPath = extractFolder;

        new File(newPath).mkdir();
        Enumeration zipFileEntries = zip.entries();

        ZipEntry entry;

        // Process each entry
        while (zipFileEntries.hasMoreElements())
        {
            // grab a zip file entry
            entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            File destFile = new File(newPath, currentEntry);
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory())
            {
                BufferedInputStream is = new BufferedInputStream(zip
                .getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                BUFFER);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        }
        zip.close();
    }
    catch (Exception e){
            System.out.println("ERROR: "+e.getMessage());
    }
}