2012-02-01 59 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