停止/启动网络连接

时间:2012-06-11 10:27:53

标签: java

我有一种从URL下载文件的方法。我需要编写单元测试用例来恢复下载,那么有没有办法禁用/启用网络连接,以便恢复下载方法可以调用?

我的下载方法如下:

public String download(LatestVersionInfo info) throws FileNotFoundException, XMLStreamException,NoSuchAlgorithmException, MalformedURLException {       
        boolean isCached = checkCache(info, location);
        String filePath = null;
        RandomAccessFile randomAccessFile = null;
        InputStream inputStream = null;
        if (!isCached) {
            try {
                createLocation();
                int responseCode = 0;
                HttpsURLConnection connection = getSSLCertificate(info
                        .getLatestVersionUrl().toString());
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Range", "bytes=" + downloaded
                        + "-");
                connection.connect();
                responseCode = connection.getResponseCode();
                if (responseCode / 100 != 2) {
                    error();
                }
                int contentLength = connection.getContentLength();
                if (contentLength < 1) {
                    error();
                }
                if (size == -1) {
                    size = contentLength;
                    stateChanged();
                }
                randomAccessFile = new RandomAccessFile(
                        getDownLoadPath(info.getLatestVersionUrl()), "rw");
                randomAccessFile.seek(downloaded);
                inputStream = connection.getInputStream();
                while (status == DOWNLOADING) {
                    byte buffer[];
                    float finalSize = size - downloaded;
                    if (finalSize > MAX_BUFFER_SIZE) {
                        buffer = new byte[(int) finalSize];
                    } else {
                        buffer = new byte[MAX_BUFFER_SIZE];
                    }
                    int read = inputStream.read(buffer);
                    if (read == -1)
                        break;

                    randomAccessFile.write(buffer, 0, read);
                    downloaded += read;
                    stateChanged();
                }

                if (status == DOWNLOADING) {
                    status = COMPLETE;
                    stateChanged();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (randomAccessFile != null)
                        randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (inputStream != null)
                        inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }           
        }
        return filePath;
    }

有没有办法禁用/启用网络连接,以便恢复下载方法可以调用。

感谢。

1 个答案:

答案 0 :(得分:1)

看起来你试图在连接失败后恢复,而不是以某种方式暂停底层的TCP传输(这会更难)。

所以你可以在中途杀死HttpsURLConnection(来自一个单独的线程)。如果将HttpsURLConnection connection放在字段中,则另一个线程可以调用connection.getInputStream().close(),这将导致连接“失败”。