2012-02-01 62 views
1

我试图读取从CSV数据(股票价格)在互联网上提交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"); 
    } 
} 

任何想法?

回答

2

实际上似乎有与服务器的问题。如果您跟踪连接,你会发现,反应是:

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()); 

:您可以通过添加以下行做到这一点。

1

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

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

0

看起来你retreive一些JavaScript,而不是你想要的数据检查URL

+0

当我从浏览器访问链接时,它通常会给我csv文件:( – 2012-02-01 08:52:27

+0

在浏览器中启用HTTP监视或使用'curl'等工具检查所做HTTP请求的详细信息。 – 2012-02-01 08:54:01