Java从网络读取CSV文件 [英] Java read CSV file from the web
问题描述
我正在尝试从网络读取 CSV 文件.这是我编写的 Java 代码:
I'm trying to read a CSV file from the web. Here's the Java code I have written:
String st = "http://finance.yahoo.com/d/quotes.csv?s=NAK&f=sl1c1vd1t1p2hg&e=.csv";
URL stockURL = new URL(st);
BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openStream()));
String s = null;
while ((s=in.readLine())!=null) {
System.out.println(s);
}
然而,BufferedReader
似乎是空的.当我将 URL 放入浏览器时,会下载一个 CSV 文件,该文件不是空文件.有任何想法吗?
However, the BufferedReader
appears to be empty. When I put the URL in my browser, a CSV file is downloaded which is not an empty file. Any ideas?
推荐答案
服务器实际上发送了一个 301 响应重定向到 http://download.finance.yahoo.com/d/quotes.csv?s=NAK&f=sl1c1vd1t1p2hg&e=.csv
.您的浏览器遵循重定向,但您的代码不遵循.如果您直接使用最终到达网址,则您的代码可以正常工作.
The server actually sends a 301 response redirecting to http://download.finance.yahoo.com/d/quotes.csv?s=NAK&f=sl1c1vd1t1p2hg&e=.csv
. Your browser follows the redirect, but your code does not. If you directly use the final URL, your code works fine.
这篇关于Java从网络读取CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!