Java 从Internet读取CSV文件

Java 从Internet读取CSV文件,java,Java,我试图从互联网上的CSV文件中读取数据(股票价格),但我收到了如下信息: (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-an

我试图从互联网上的CSV文件中读取数据(股票价格),但我收到了如下信息:

(function() {
    var ga = document.createElement('script');
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
})();
这是我的密码:

public static void main(String[] args) {
    try {
        URL url12 = new URL("http://www.cophieu68.com/export/excel.php?id=BBS");
        URLConnection urlConn = url12.openConnection();
        InputStreamReader inStream = new InputStreamReader(urlConn.getInputStream());
        BufferedReader buff = new BufferedReader(inStream);
        String content1 = buff.readLine();
        String content2 = buff.readLine();
        while (content2 != null) {
            System.out.println(content2);
            content2 = buff.readLine();
        }
    } catch (Exception e) {
        System.out.print("KHONG TAI DUOC DU LIEU");
    }
}

有什么想法吗?

您正试图读取一个
.csv
文件,但您使用的链接实际上是一个将您重定向到csv的网页


检查重定向或使用其他链接(:

看起来您检索了一些Javascript而不是所需的数据。请检查您的URL。

实际上服务器似乎有问题。如果跟踪连接,您会注意到响应是:

HTTP/1.0 302 Moved Temporarily
Date: Wed, 01 Feb 2012 08:48:48 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.1.6
Pragma: no-cache
Expires: 0
Cache-Control: must-revalidate, post-check=0, pre-check=0
Content-Description: File Transfer
Content-Disposition: csv; filename=excel_bbs.csv; size=46925
Set-Cookie: PHPSESSID=2lgqidrmqrvu3piu47ulvrn5t3; path=/
Set-Cookie: cophieu68_LanuageView=vn; expires=Fri, 02-Mar-2012 08:48:48 GMT
Location: http://www.cophieu68.com/wap
...
如果您遵循重定向链接(http://www.cophieu68.com/wap)您将获得您的客户端正在接收的内容。我不知道服务器为何以这种方式设置,但如果您在客户端中禁用重定向,您将收到您的CSV。您可以通过添加以下行来完成此操作:

URLConnection urlConn = url12.openConnection();
// Disable redirects
HttpURLConnection conn = (HttpURLConnection)urlConn;
conn.setInstanceFollowRedirects( false );
InputStreamReader inStream = new InputStreamReader(urlConn.getInputStream());

如果您控制了服务器,请检查您是否真的需要发送此响应。

当我从浏览器访问链接时,它通常会给我csv文件:(在浏览器中启用HTTP监控,或使用
curl
之类的工具检查所做HTTP请求的详细信息。