2017-11-07 25 views
0

タイムアウトになりました - NSE BhavcopyのHttpURLConnectionを使用してファイルをダウンロードします - 接続が、私はURLからファイルをダウンロードするためにJavaプログラムを書かれているエラー

しかし、私は「『メイン』 java.net.ConnectExceptionスレッドの例外を取得しています:接続をタイムアウト:接続 "、私は次の順列を試みた&の組み合わせが、運がない。プロキシ&で

  1. プロキシ
  2. なし認証&では、次の異なるURLの

で試してみました

  • せずに参照用コード&トレースで、いくつかのいずれかが私を助けることができるしてください

    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) 
    
    +0

    問題は私のために働いたとは言えません。ブラウザでURLを貼り付けてダウンロードするかどうか試しましたか? – markbernard

    +0

    はい、URLをブラウザに貼り付けると機能しています。私は書かれているNETの同等のコードは、うまく動作しているwebclientを使用しています。 – sskini

    +0

    最後に私はいくつかの解決策を見つけました – sskini

    答えて

    0

    downloadFile( "http://www.bseindia.com/download/BhavCopy/Equity/EQ_ISINCODE_071117.zip"、 "D:\市場は\ 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()); 
        } 
    } 
    
    関連する問題