当前位置: 首页 > 知识库问答 >
问题:

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

查飞星
2023-03-14

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

但是我得到了“thread'main'java.net.ConnectException:Connection timed Ut:Connect”,我尝试了以下排列和组合,但没有成功。

    null

以下是代码和跟踪以供参考,有人可以帮助我吗

 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个答案

胡修伟
2023-03-14

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());
    }
}
 类似资料:
  • 问题内容: 是否可以获取通过HttpURLConnection下载的文件的名称? 在上面的示例中,我无法从URL中提取文件名,但是服务器会以某种方式向我发送文件名。 问题答案: 您可以使用HttpURLConnection.getHeaderField(String name) 获取标头,该标头通常用于设置文件名: 正如其他答案指出的那样,服务器可能返回无效的文件名,但是您可以尝试使用它。

  • 用于图像文件: 以下是我完成的错误: java.net.socketException:sendto失败:在libcore.io.iobridge.MaybeThrowAfterSendto(Iobridge.java:586)在libcore.io.iobridge.sendto(Iobridge.java:555)在java.net.plainsockeTimpl.write(plainsoc

  • 我在用NodeMailer发邮件。我一直在成功地使用我的gmail帐户发送电子邮件。我刚刚通过GoDaddy转到了我的域名自定义电子邮件。此电子邮件通过Hotmail运行。现在我已经做了这个切换,我得到的错误是:每次连接超时。Gmail还能用,但我不能用hotmail。 有什么解决方法吗?NodeMailer有什么更好的电子邮件选择吗? 这是我的设置:

  • 问题是,我想写一个Java程序来增加特定URL的页面点击量。目前我只是在网上冲浪,并得到一些基本的程序来做测试。然而,我甚至无法使程序运行...下面是我的程序和相关输出。 Eclipse、Java程序: 以下是输出: 线程“main”java.net.ConnectException:连接超时:java.net.plainsockeTimpl.SocketConnect(本机方法)java.net

  • 问题内容: 我正在使用selenium对超链接执行单击功能,该超链接已加载在特定页面上。该脚本适用于Google chrome,但不适用于phantomjs。为什么这不起作用? 这在chrome中有效,但始终会打开一个新的chrome窗口以完成任务。我读到我应该使用phantomjs使其在后台运行,但是当我将驱动程序切换到phantomjs时,下载似乎没有完成。屏幕截图已抓取,并且确实在正确的页面

  • 我正在使用resin服务器+spring框架和c3p0连接池。我已经使用以下属性文件配置了连接池。但不知怎的,每隔24小时左右,我的网站就会出现连接超时错误,然后我不得不重新启动我的resin服务器,让网站重新运行。请告诉我在下面的配置文件中有什么错误,以及我在这里缺少了什么。