2015-03-30 41 views
0

我使用this類在HTTP上傳文件,我的問題是,我注意到,當我從我的路由器將LAN電纜上傳stucks作爲寫調用塊,下面顯示了它正好卡住,我嘗試中斷線程,但這也沒有幫助。中斷的OutputStream - Android電子

現在的問題是我怎麼能中斷的OutputStream?

public void addFilePart(String fieldName, File uploadFile) 
     throws IOException { 
    ... 
    //removed code from here for clarity. 
    //Can be found in the link above 

    FileInputStream inputStream = new FileInputStream(uploadFile); 
    byte[] buffer = new byte[4096]; 
    int bytesRead = -1; 
    while ((bytesRead = inputStream.read(buffer)) != -1) { 
     //The code stuck here. 
     //using if (Thread.interrupted()) doesn't help 
     //as the write blocks when the network cable 
     //removed from the router 
     outputStream.write(buffer, 0, bytesRead); 
    } 
    outputStream.flush(); 
    inputStream.close(); 

    writer.append(LINE_FEED); 
    writer.flush();  
} 
+0

爲什麼用'outputstream'上傳文件通過HTTP?爲什麼不使用網絡連接管理器?它會自動確定是否發生錯誤。 – 2015-03-31 00:02:38

+0

我應該使用什麼來幫助我滿足我的需求? – 2015-03-31 00:03:12

+0

它將疏通並拋出IOException異常':幾分鐘後連接reset'。 – EJP 2015-03-31 00:35:41

回答

0

我發現迄今最好的解決辦法是做HttpURLConnection.disconnect()從另一個線程,這似乎是工作的罰款。