Java scanner does not read specific file
我有一个Java软件,它既要求雅虎财务的当前股价,又要求历史股价。
正如其他文章所描述的,雅虎可以将价格写入一个文件,这个文件可以被扫描仪读取。要请求亚马逊当前的价格,我打电话:http://finance.yahoo.com/d/quotes.csv?s=amz.de&f=snl1t1c4
要请求亚马逊在过去5年的价格,我打电话给:http://ichart.finance.yahoo.com/table.csv?s=amz.d e&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=cvs
如果我在浏览器中访问这两个链接,它将下载一个.csv文件,其中每个文件都包含预期的数据。
在Java中,相应的.CSV文件应该通过FLULIN方法读取:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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; } |
其中请求是上面的链接之一。
我用这个软件有几个星期了,它运行得很好。但在过去的几天里,它不再适用于历史数据,但对于当前的数据,它可以正常工作。
使用指向历史数据的链接,扫描仪将正常打开,不会引发异常,但scanner.hasnextline()将立即返回false,但使用浏览器下载的.csv文件有1305行。
你们中有人明白为什么扫描器不再接受.csv文件作为历史数据,而是接受当前数据?
原因是当您调用http://ichart.finance.yahoo.com/table.csv时?s=amz.d e&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.d e&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=.cvs(即返回代码301),但从旧URL生成的输入流将为空。如果要模拟浏览器的功能,则必须发送HTTP GET请求,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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(); } } } |
号
请用"https"更新您的URL,然后重试。
旧:http://ichart.finance.yahoo.com/table.csv?s=amz.d e&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.d e&d=3&e=20&f=2017&a=3&b=20&c=2012&g=d&ignore=cvs
1 2 3 4 | 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 |