首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中下载NSE Bhavcopy (NSE市场收盘价)?

如何在Java中下载NSE Bhavcopy (NSE市场收盘价)?
EN

Stack Overflow用户
提问于 2017-10-31 18:31:41
回答 1查看 1.2K关注 0票数 0

我需要从下面的Java链接下载一个文件

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

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

代码语言:javascript
复制
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);
EN

回答 1

Stack Overflow用户

发布于 2017-11-08 18:06:03

我自己找到了一个解决方案

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

代码语言:javascript
复制
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());
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47033139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档