Java 扫描器不读取特定文件

Java scanner does not read specific file

我有一个 java 软件,它要求雅虎财经提供当前和历史股票价格。

如其他帖子所述,yahoo 可以将价格写入文件,扫描仪可以读取该文件。 要询问亚马逊的当前价格,我会致电: http://finance.yahoo.com/d/quotes.csv?s=AMZ.DE&f=snl1t1c4

要查询亚马逊过去 5 年的价格,我致电: http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs

如果我在浏览器中访问这两个 link,它会下载一个 .csv 文件,其中包含每个文件的预期数据。

在java中对应的.csv文件需要通过以下方式读取:

private static List<String> requestStockData(String request) throws IOException {
    Scanner scanner = null;
    List<String> answer = new ArrayList();
    try {
        scanner = new Scanner(new URL(request).openStream());//no exception here
    } catch (FileNotFoundException e) {
        Tools.printDebugMessage("Received null-answer for request " + request);
        return answer;
    }
    while (scanner.hasNextLine()) {//scanner.hasNextLine() returns false
        String value = scanner.nextLine();
        answer.add(value);
        Tools.printDebugMessage("received answer from YAHOO! Finance: " + value);
    }
    scanner.close();
    return answer;
}

其中请求是上述 link 之一。

我已经使用这个软件几个星期了,它运行得非常好。 但是最近几天它不再适用于历史数据,但它适用于当前数据。

对历史数据使用link扫描器会正常打开,不会抛出异常,但是scanner.hasNextLine()会return立即为假,但是.csv使用我的浏览器下载的文件有 1305 行。

有没有人明白为什么扫描仪不再接受历史数据的 .csv 文件,而是接受当前数据的文件?

将您的 url 更新为 "https",然后重试。

旧:http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs

新:https://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs

received answer from YAHOO! Finance: Date,Open,High,Low,Close,Volume,Adj Close
received answer from YAHOO! Finance: 2017-04-19,844.95,849.35,842.90,847.90,1700,847.90
received answer from YAHOO! Finance: 2017-04-18,849.50,851.00,841.25,845.00,3100,845.00
received answer from YAHOO! Finance: 2017-04-17,839.90,839.90,839.90,839.90,000,839.90

原因是,当您调用 http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs the browser is redirected to https://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs(即返回代码 301)时,从旧的 URL 生成的输入流将为空。如果你想模拟你的浏览器做什么,你必须发送一个 HTTP get 请求,例如像这样:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

public class Main {
    public static void main(String [] args){
        String request = "http://ichart.finance.yahoo.com/table.csv?s=AMZ.DE&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs";
        try {
            HttpGet httpget = new HttpGet(request);         
            HttpResponse response = HttpClients.createDefault().execute(httpget);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            String filePath = "output.csv";
            FileOutputStream fos = new FileOutputStream(new File(filePath));
            int inByte;
            while((inByte = is.read()) != -1)
                 fos.write(inByte);
            is.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}