2017-11-07 12 views
0

시간 초과 - NSE BhavcopyHttpURLConnection의를 사용하여 파일을 다운로드 - 연결 내가 URL에서 파일을 다운로드하는 자바 프로그램을 작성했습니다 오류

하지만 " '주'java.net.ConnectException 스레드의 예외를 얻고있다 : 연결 시간 초과 : 연결 ", 다음과 같은 순열을 시도 & 조합하지만 행운을 빕니다.

  • 가 다른 URL의
  • 으로 시도 프록시없이 프록시 &와

    1. 인증 &와
    2. 없이 참조 코드 & 추적되는 다음, 어떤 사람은 제발 도움이 될 수 있습니다

      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을 브라우저에 붙여 넣으면 작동합니다. 난 괜찮아요 작동하는 webclient를 사용하여 writeNet 닷넷 코드가 있습니다. – 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()); 
        } 
    }