java - 输入流超时

标签 java android sockets

有没有办法为输入流设置超时?

我当前的代码如下所示:

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");

最佳答案

我认为根据您的要求,您可以使用

HttpURLConnection.setReadTimeout()

也许这些链接会对您有所帮助。

  1. Is it possible to read from a Java InputStream with a timeout?
  2. Class HttpURLConnection
  3. Can I set a timeout for a InputStream's read() function?

关于java - 输入流超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12914484/

相关文章:

java - 在 java 中使用日历或 Joda-Time

java - 如何使用 Lotus Notes API 从 Java 创建和运行代理

android - android中共享元素之上的内容转换

C/如何监听多个UDP端口

java - Maven - JSF 2.0 不适用于嵌入式 tomcat

java - MySQL Minecraft Bukkit 错误,因为没有正确执行

android - 带有操作按钮的 snackbar 未与文本对齐

android - Android Studio中带有Gradle的参数的方法

ios - 套接字创建失败但 socket() 和 bind() 方法不返回 -1

sockets - 套接字可变的 Rust 结构字段