2013-03-22 38 views
0

我已經創建了代碼來訪問我下載的文件,但這不是任務需要的。它希望我使用此處給出的URL訪問信息。我不知道爲什麼我不斷收到IO異常。爲什麼我不斷收到I/O異常?

public static void main(String[] args) throws Exception{ 

    int test = 0; 
    int sum = 0; 
    int i = 0; 
    try{ 
    java.net.URL url = new java.net.URL("http://cs.armstrong.edu/liang/data/Scores.txt"); 
    Scanner input = new Scanner(url.openStream()); 
    while (input.hasNext()){ 
     int score = input.nextInt(); 
     sum += score; 
     i++; 
    } 
    } 
    catch(java.net.MalformedURLException ex){ 
     ex.printStackTrace(); 
    } 
    catch(java.io.IOException ex){ 
    System.out.println("I/O Errors: no such file"); 
    } 


    int avg = sum/i; 
    System.out.println("The average is: " + avg); 
    System.out.println("The sum is: " + sum); 




} 

堆棧跟蹤

java.net.SocketException: Invalid argument: connect 
at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
at java.net.PlainSocketImpl.connect(Unknown Source) 
at java.net.SocksSocketImpl.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at sun.net.NetworkClient.doConnect(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.openServer(Unknown Source) 
at sun.net.www.http.HttpClient.<init>(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.http.HttpClient.New(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at java.net.URL.openStream(Unknown Source) 
at readfile.main(readfile.java:15) 

異常在線程 「主」 java.lang.ArithmeticException:/由零 在readfile.main(readfile.java:30)

+0

您是否能夠從瀏覽器中打開網址? – Anubhab 2013-03-22 06:20:51

+2

您可以打印該例外的堆棧跟蹤嗎? – 2013-03-22 06:20:57

+0

我可以從瀏覽器中找到網址。 – BluceRee 2013-03-22 06:23:15

回答

0

的代碼是完全相同精細。只需在連接之前添加以下部分即可設置代理詳細信息。

//The follwing settings is needed for Connecting to internet from Local Network 
System.getProperties().put("http.proxyHost", "ip.add.of.proxy"); 
System.getProperties().put("http.proxyPort", "80"); 
System.getProperties().put("http.proxyUser", "user_name"); // Your username 
System.getProperties().put("http.proxyPassword", "*************"); // your password 

而且也動下面的代碼的嘗試捕捉內部塊否則,如果連接失敗,你會得到​​例外。

int avg = sum/i; 
System.out.println("The average is: " + avg); 
System.out.println("The sum is: " + sum); 
相關問題