Java 停止/启动网络连接

Java 停止/启动网络连接,java,Java,我有一个从URL下载文件的方法。我需要编写单元测试用例来恢复下载,那么有没有办法禁用/启用网络连接以便恢复下载方法可以调用 我的下载方法如下: public String download(LatestVersionInfo info) throws FileNotFoundException, XMLStreamException,NoSuchAlgorithmException, MalformedURLException { boolean isCached

我有一个从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;
    }
public String download(最新版本信息)引发FileNotFoundException、XMLStreamException、NoSuchAlgorithmException、MalformedURLException{
布尔值isCached=checkCache(信息、位置);
字符串filePath=null;
RandomAccessFile RandomAccessFile=null;
InputStream InputStream=null;
如果(!isCached){
试一试{
createLocation();
int-responseCode=0;
HttpsURLConnection connection=getSSLCertificate(信息
.getLatestVersionUrl().toString());
connection.setRequestMethod(“GET”);
connection.setRequestProperty(“范围”、“字节数=“+下载
+ "-");
connection.connect();
responseCode=connection.getResponseCode();
如果(响应代码/100!=2){
错误();
}
int contentLength=connection.getContentLength();
if(contentLength<1){
错误();
}
如果(大小==-1){
大小=内容长度;
stateChanged();
}
randomAccessFile=新的randomAccessFile(
getDownLoadPath(info.getLatestVersionUrl()),“rw”);
randomAccessFile.seek(下载);
inputStream=connection.getInputStream();
while(状态==下载){
字节缓冲区[];
float finalSize=大小-下载;
如果(最终化>最大缓冲区大小){
缓冲区=新字节[(int)finalSize];
}否则{
缓冲区=新字节[最大缓冲区大小];
}
int read=inputStream.read(缓冲区);
如果(读取==-1)
打破
randomAccessFile.write(缓冲区,0,读取);
下载+=读取;
stateChanged();
}
如果(状态==正在下载){
状态=完成;
stateChanged();
}
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
if(randomAccessFile!=null)
randomAccessFile.close();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
如果(inputStream!=null)
inputStream.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}           
}
返回文件路径;
}
是否有任何方法禁用/启用网络连接,以便可以调用resume download方法


谢谢。

看起来您是在尝试在连接失败后恢复,而不是以某种方式暂停底层TCP传输(这将更加困难)


因此,您可以在中途(从单独的线程)终止
HttpsURLConnection
。如果您将
HttpsURLConnection连接
放在一个字段中,那么另一个线程可以调用
connection.getInputStream().close()
,这将导致连接“失败”。

看起来您是在尝试在连接失败后恢复连接,而不是以某种方式暂停底层TCP传输(这将更加困难)


因此,您可以在中途(从单独的线程)终止
HttpsURLConnection
。如果将
HttpsURLConnection连接
放在字段中,则另一个线程可以调用
connection.getInputStream().close()
,这将导致连接“失败”。

“可以调用”什么?这里的问题是什么?“可以调用”什么?这里有什么问题?