有沒有辦法將InputStream設置爲超時?InputStream超時
我當前的代碼看起來像這樣:
public class DownloadFile implements Runnable {
private EventHandler eh;
private String source;
private String destination;
public void run(){
try {
Log.d("Download", "Download... " + source);
URL url = new URL(source);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(destination + "t");
1.另外,如果在所需時間內沒有發生連接,還要使用connectionTimeout()來關閉套接字。 2.想處理「慢服務器」情況?即在每'x'秒後發送小塊數據的服務器。這樣的服務器會每次重置readTimeout,因此整個「讀取」操作將花費相當長的時間。爲了處理這種情況,使用兩個線程,第一個線程將處理您的「讀取」操作。第二個線程會睡眠'y'秒。當它醒來時,如果第一個線程仍在運行,它會殺死第一個線程。 –