10%的进度停止下载同一个文件(仅在少数机器上)

vktxenjb  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(191)

我的应用程序似乎可以在大多数机器上运行,但有些只是不想合作。。。服务器上的文件很好,服务器本身没有关闭或者其他什么。问题是,除了一个文件(12mb)外,应用程序正确下载了所有必需的文件(高达600kb)。我搞不懂怎么了。我猜是有什么东西在后台运行,阻止了我的应用程序下载线程,或者路由器阻止了一些数据包?我只能看到进度条上升到4%(有时甚至上升到8!)然后停止,没有任何错误或异常,而所有其他下载上升到100%没有问题。有什么想法吗?
下面是我用来下载文件的类(我从http://www.java-tips.org/java-se-tips/javax.swing/how-to-create-a-download-manager-in-java.html):

class Download extends Observable implements Runnable {
  private static final int MAX_BUFFER_SIZE = 1024;

  public static final String STATUSES[] = { "Pobieranie", "Pauza", "OK", "Anulowany",
      "Błąd" };

  public static final int DOWNLOADING = 0;

  public static final int PAUSED = 1;

  public static final int COMPLETE = 2;

  public static final int CANCELLED = 3;

  public static final int ERROR = 4;

  private URL url; // download URL

  private int size; // size of download in bytes

  private int downloaded; // number of bytes downloaded

  private int status; // current status of download

  // Constructor for Download.
  public Download(URL url) {
    this.url = url;
    size = -1;
    downloaded = 0;
    status = DOWNLOADING;
    System.err.println("==========Pobieram plik: " + url.toString());

    // Begin the download.
    download();
  }

  // Get this download's URL.
  public String getUrl() {
    //return url.toString();
      return getFileName(url);
  }

  // Get this download's size.
  public int getSize() {
    return size;
  }

  // Get this download's progress.
  public float getProgress() {
    return ((float) downloaded / size) * 100;
  }

  public int getStatus() {
    return status;
  }

  public void pause() {
    status = PAUSED;
    stateChanged();
  }

  public void resume() {
    status = DOWNLOADING;
    stateChanged();
    download();
  }

  public void cancel() {
    status = CANCELLED;
    stateChanged();
  }

  private void error() {
    status = ERROR;
    stateChanged();
  }

  private void download() {
    Thread thread = new Thread(this);
    thread.start();
  }

  // Get file name portion of URL.
  private String getFileName(URL url) {
    String fileName = url.getFile();
    return fileName.substring(fileName.lastIndexOf('/') + 1);
  }

  // Download file.
  public void run() {
    RandomAccessFile file = null;
    InputStream stream = null;

    try {
      // Open connection to URL.
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();

      // Specify what portion of file to download.
      connection.setRequestProperty("Range", "bytes=" + downloaded + "-");

      // Connect to server.
      connection.connect();

      // Make sure response code is in the 200 range.
      if (connection.getResponseCode() / 100 != 2) {
        error();
      }

      // Check for valid content length.
      int contentLength = connection.getContentLength();
      if (contentLength < 1) {
        error();
      }

      /*
       * Set the size for this download if it hasn't been already set.
       */
      if (size == -1) {
        size = contentLength;
        stateChanged();
      }

      File f = new File(Files.download_dir + Files.SEPARATOR + getFileName(url));
      if (f.exists()) f.delete();
      // Open file and seek to the end of it.
      file = new RandomAccessFile(Files.download_dir + Files.SEPARATOR + getFileName(url), "rw");
      file.seek(downloaded);

      stream = connection.getInputStream();
      while (status == DOWNLOADING) {
        /*
         * Size buffer according to how much of the file is left to download.
         */
        byte buffer[];
        if (size - downloaded > MAX_BUFFER_SIZE) {
          buffer = new byte[MAX_BUFFER_SIZE];
        } else {
          buffer = new byte[size - downloaded];
        }

        // Read from server into buffer.
        int read = stream.read(buffer);
        if (read == -1)
          break;

        // Write buffer to file.
        file.write(buffer, 0, read);
        downloaded += read;
        //System.out.println("Pobralem juz: " + String.valueOf(downloaded));
        stateChanged();
      }

      /*
       * Change status to complete if this point was reached because downloading
       * has finished.
       */
      if (status == DOWNLOADING) {
        status = COMPLETE;
        stateChanged();
      }
    } catch (Exception e) {
      error();
    } finally {
      // Close file.
      if (file != null) {
        try {
          file.close();
        } catch (Exception e) {
        }
      }

      // Close connection to server.
      if (stream != null) {
        try {
          stream.close();
        } catch (Exception e) {
        }
      }
    }
  }

  private void stateChanged() {
    setChanged();
    notifyObservers();
  }
}

(如果您需要更多的代码,请告诉我,我会将所有内容上传到某个地方。)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题