如何修改此方法以使连接仅在有限的时间内保持连接?我希望在5秒连接后获得输出流。
public boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
HttpURLConnection urlConnection = null;
try {
final URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = urlConnection.getInputStream();
// you can use BufferedInputStream and BufferOuInputStream
IOUtils.copy(stream, outputStream);
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(stream);
Log.i(getTag(), "Stream closed all done");
return true;
} catch (final IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
IOUtils.close(urlConnection);
}
return false;
}