簡體   English   中英

如何使用httpurlconnection從InputStream讀取時設置超時?

[英]How to set a timeout while reading from InputStream using httpurlconnection?

我有一個java應用程序,我在其中使用HttpURLConnection下載文件。 我在連接超時中設置了15秒,在讀取超時屬性中設置了1小時。 根據我的理解,如果服務器上的文件足夠大,下載時間超過1小時,則會因超時異常而失敗。 工作良好。 問題是,當我在下載過程中從客戶端計算機拔出Internet電纜(從InputStream讀取緩沖區)時,下載過程不會立即終止,需要1小時(讀取超時)來中斷下載。 有什么辦法可以終止下載過程嗎? 以下是源代碼:

public class HttpDownloadUtility {
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir)
            throws IOException {
        URL url = new URL(fileURL);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setConnectTimeout(15*1000);
        httpConn.setReadTimeout(60*60*1000);
        int responseCode = httpConn.getResponseCode();

        // always check HTTP response code first
        if (responseCode == HttpURLConnection.HTTP_OK) {
            String fileName = "";
            String disposition = httpConn.getHeaderField("Content-Disposition");
            String contentType = httpConn.getContentType();
            int contentLength = httpConn.getContentLength();

            if (disposition != null) {
                // extracts file name from header field
                int index = disposition.indexOf("filename=");
                if (index > 0) {
                    fileName = disposition.substring(index + 10,
                            disposition.length() - 1);
                }
            } else {
                // extracts file name from URL
                fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                        fileURL.length());
            }

            System.out.println("Content-Type = " + contentType);
            System.out.println("Content-Disposition = " + disposition);
            System.out.println("Content-Length = " + contentLength);
            System.out.println("fileName = " + fileName);

            // opens input stream from the HTTP connection
            InputStream inputStream = httpConn.getInputStream();
            String saveFilePath = saveDir + File.separator + fileName;

            // opens an output stream to save into file
            FileOutputStream outputStream = new FileOutputStream(saveFilePath);

            int bytesRead = -1;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = inputStream.read(buffer)) != -1) { //This is where the download gets stuck on some connection issues 
                outputStream.write(buffer, 0, bytesRead);
            }

            outputStream.close();
            inputStream.close();

            System.out.println("File downloaded");
        } else {
            System.out.println("No file to download. Server replied HTTP code: " + responseCode);
        }
        httpConn.disconnect();
    }
}

根據我的理解,如果服務器上的文件足夠大,下載時間超過1小時,則會因超時異常而失敗。

不可以。如果服務器在一個多小時內沒有響應任何單個讀取請求,它將失敗。 當您調用read()時超時開始,並在收到對該讀取的響應或超時時間到期時結束。 然后它再次開始下一次讀取。 總時間與它無關。 你完全誤解了。

工作良好。

工作正常但不如你所描述的那樣。

問題是,當我在下載過程中從客戶端計算機拔出Internet電纜(從InputStream讀取緩沖區)時,下載過程不會立即終止,需要1小時(讀取超時)來中斷下載。

如上所述,這正是它應該做的事情。

有什么辦法可以終止下載過程嗎?

設置較短的超時。 設置它足夠長,以便服務器應該在該間隔內響應,並且足夠短的電纜拉動不需要太長時間才能超時。 一個小時太長了。 試試幾分鍾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM