⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contentdownload.java

📁 基于jxta的局域网P2P文件共享,可以实现局域网中的文件p2p共享,实现文件快速传输及交流
💻 JAVA
字号:
package connex.plugins.filesharing;

import net.jxta.share.client.GetContentRequest;
import net.jxta.share.ContentAdvertisement;
import java.io.File;
import net.jxta.peergroup.PeerGroup;
import java.lang.reflect.InvocationTargetException;

import javax.swing.ImageIcon;
import connex.plugins.filesharing.model.DownloadRow;
import connex.plugins.filesharing.ui.DownloadPanel;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author Hisham Khalil
 * @version 1.0*/
public class ContentDownload
    extends GetContentRequest {
  private ContentAdvertisement searchResult = null;
  private DownloadRow myRow;
  private ImageIcon down = new ImageIcon(ContentDownload.class.getResource("downloading_small.png"));
  private ImageIcon complete = new ImageIcon(ContentDownload.class.getResource("ok.png"));
  private ImageIcon close = new ImageIcon(ContentDownload.class.getResource("delete_small.png"));
  private ImageIcon cancel = new ImageIcon(ContentDownload.class.getResource("delete_small.png"));
  private long curTime, newTime;

  private long oSize;

  /**
   * @directed
   * @link aggregationByValue
   */
  private DownloadMonitor dm;

  public ContentDownload(PeerGroup pg, ContentAdvertisement[] res) throws
      InvocationTargetException {

    this(pg, res, new File(res[0].getName()));
  }

  public ContentDownload(PeerGroup pg, ContentAdvertisement[] res,
                         File tmpFile) throws InvocationTargetException {
    super(pg, res, tmpFile);

    this.searchResult = res[0];

    this.myRow = new DownloadRow(searchResult.getName(),
                                 searchResult.getContentId().toString());

    curTime = System.currentTimeMillis();

    init();
    dm = new DownloadMonitor(myRow, this.getFile(), getOriginalLength());
    dm.start();

  }

  private void init() {
    down.setDescription("Downloading");
    complete.setDescription("Download Completed");
    close.setDescription("Download failur");
    cancel.setDescription("Canceled");
    myRow.setState("0", down);

  }

  public ContentAdvertisement getContentAdvertisement() {
    return searchResult;
  }

  public long getOriginalLength() {
    return searchResult.getLength();
  }

  public void notifyDone() {
    newTime = System.currentTimeMillis();
    int m = (int) (newTime - curTime);
    try {
      dm.interrupt();
      dm.join();
    }
    catch (InterruptedException ex) {
    }
    myRow.setState("Total time " + restTime(m), complete);
    myRow.setFinished(true);

    if (DownloadPanel.getInstance().isFinishedToClear()) {

      myRow.remove();
      myRow.clear();
      myRow = null;
    }
    FilesharingService.getInstance().shareContent(new ShareFile(this.getFile().getAbsolutePath()));

    //dm.stopit() ;

  }

  public void notifyFailure() {
    dm.stopit();
    dm.interrupt();
    myRow.setFailed(true);
    myRow.setState("0", close);
    if (DownloadPanel.getInstance().isFailedToClear()) {

      myRow.remove();
      myRow.clear();
      myRow = null;
    }
  }

  public void cancel() {
    if (myRow.isDownloaded() || myRow.isFailed()) {
      return;
    }
    super.cancel();

    dm.stopit();
    dm.interrupt();

    myRow.setCancel(true);
    myRow.setState("0", cancel);
    //myRow.remove();
    if (DownloadPanel.getInstance().isCanceledToClear()) {

      myRow.remove();
      myRow.clear();
      myRow = null;
    }

  }

  public void notifyUpdate(int per) {

  }

  private String restTime(int t) {
    t /= 1000;
    String rest = t + " sec";
    if ( (t >= 60) && (t < 3600)) {

      rest = (t / 60) + " min ";
      int sec = t % 60;

      rest += sec + " sec";
    }
    if (t >= 3600) {
      rest = (t / 3600) + " h ";
      int min = t % 3600;
      rest += (min / 60) + " min ";
      int sec = min % 60;
      rest += sec + " sec";
    }
    return rest;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -