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

📄 sharemanager.java

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

import java.io.*;

import javax.swing.*;

import net.jxta.document.*;
import net.jxta.share.*;
import net.jxta.share.metadata.*;

public class ShareManager
    implements Runnable {
  /**
   * @directed
   * @link aggregationByValue
   */
  CMService myCMS;
  MimeMediaType type = new MimeMediaType("text", "xml");

  /**
   * @directed
   */
  private ShareFile file;
  private DefaultListModel fileList;
  //private File files[] = null;
  JFileChooser fc = new JFileChooser();
  private boolean run = false;
  private Thread shareThread=null;

  public ShareManager(CMService myCMS, DefaultListModel fileList) {
    this.myCMS = myCMS;
    this.fileList = fileList;
  }

  public void share(ShareFile file) {
    this.file = file;

     shareThread = new Thread(this);


    shareThread.start();
  }

  protected boolean isRunning() {
    if(shareThread==null){
      return false;
    }
    return shareThread.isAlive();
  }

  public void run() {

    shareContent(file);

    //th = null;

  }

  private void shareContent(ShareFile file) {
    try {
      if (file.isDirectory()) {
        File[] files = file.listFiles();
        //Share a default Directory
        int length = files.length;
        for (int i = 0; i < length; i++) {

          ShareFile tmpfile = new ShareFile(files[i].getPath());

          shareContent(tmpfile);

        }
      }
      else {

        String fileType = myCMS.getContentManager().getMimeType(
            file);
        StructuredDocument doc = StructuredDocumentFactory.
            newStructuredDocument(type,
                                  "metadata",
                                  FilesharingService.getInstance().
                                  getWorkspace().getPeerAdv().getName());

        ContentMetadata[] md = {
            ContentMetadataFactory.newInstance(doc)};
        FileContent f = myCMS.getContentManager().share(
            file, file.getName().toString(),
            fileType, md);

        try {
          Icon ico = fc.getIcon(file);
          file.setContent( (Content) f);
          file.setIcon(ico);
          fileList.addElement(file);
        }
        catch (Exception ex) {
        }

      }
    }
    catch (IOException e) {
      System.out.println("ERROR: could not share ");
      e.printStackTrace();
      return;
    }

  }

  /**
   *
   * @param file ShareFile
   */
  public void unShare(ShareFile file) {

    try {

      myCMS.getContentManager().unshare(file.getContent());

    }
    catch (Exception e) {}

  }

  /**
   *
   */

  public void unshareAll() {
    File f = new File("client/shares.ser");
    f.delete();
    int size = fileList.size();
    for (int i = 0; i < size; i++) {

      try {

        myCMS.getContentManager().unshare( ( (ShareFile) fileList.
                                            elementAt(i)).getContent());

      }
      catch (IOException ex) {
      }

    }
    fileList.removeAllElements();

  }

  /**
   *
   */

  public void loadContents() {

    ShareFile file;
    Content[] con;

    con = myCMS.getContentManager().getContent();
    if (con.length < 1) {
      return;
    }

    for (int i = 0; i < con.length; i++) {

      try {
        file = new ShareFile(con[i].toString().substring(6,
            con[i].toString().indexOf(";")));

        Icon ico = fc.getIcon(file);
        file.setContent(con[i]);
        myCMS.publishContent(con[i]);
        file.setIcon(ico);
        fileList.addElement(file);
      }
      catch (Exception ex) {
      }

    }

  }

}

⌨️ 快捷键说明

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