如何用Java下载NSE Bhavcopy(NSE市场收盘价)?

编程入门 行业动态 更新时间:2024-10-23 05:37:29
本文介绍了如何用Java下载NSE Bhavcopy(NSE市场收盘价)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要通过以下Java链接下载文件

I need to download a file from the following link in Java

[ www.nseindia/content/historical/EQUITIES/2017/OCT/cm30OCT2017bhav.csv.zip][1]

我有用C#编写的代码,可以有人建议使用Java等效代码

I have the code written in C#, can some one suggest Java equivalent code

WebClient webClient = new WebClient(); 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"; webClient.Headers.Add(HttpRequestHeader.Accept, accept); webClient.Headers.Add(HttpRequestHeader.UserAgent, agent); webClient.UseDefaultCredentials = true; webClient.DownloadFile(source, target);

推荐答案

我自己找到了解决方法

source =" www.bseindia/download/Bhavcopy/Derivative/bhavcopy07-11-17.zip ";target ="d:\ Market Feeds \ EQD BSE Bhavcopy \"

source = "www.bseindia/download/Bhavcopy/Derivative/bhavcopy07-11-17.zip"; target = "d:\Market Feeds\EQD BSE Bhavcopy\"

public static void downloadFileHttp(String source, String destination) throws Exception { try{ Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipaddress, port)); 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()); } }

更多推荐

如何用Java下载NSE Bhavcopy(NSE市场收盘价)?

本文发布于:2023-10-25 21:59:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收盘价   如何用   市场   NSE   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!