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

📄 fileselector.java

📁 j2me的基于http协议的断点续传。支持多线程的断点续传。
💻 JAVA
字号:
package NetFox;

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.HttpConnection;

class FileSelector extends javax.microedition.lcdui.List
    implements CommandListener {

    private final static String FILE_SEPARATOR = "/";
    
    private final BreakPoint midlet;
    private final Command goCommand = new Command("Go", Command.SCREEN, 1);
    private final Command stopCommand = new Command("Stop", Command.SCREEN, 2);
    private final Command wapCommand = new Command("Wap", Command.SCREEN, 3);
    private final Command exitCommand = new Command("Exit", Command.EXIT, 4);

    private Vector rootsList = new Vector();

    // Stores the current root, if null we are showing all the roots
    private FileConnection currentRoot = null;
    private FileConnection fileConn = null;
    private InputStream fis = null;

    SiteFileFetch fileFetch = null;
    
    FileSelector(BreakPoint midlet) {
        super("Break Point", List.IMPLICIT);
        this.midlet = midlet;
    
        loadRoots();
        displayAllRoots();
        addCommand(goCommand);
        addCommand(stopCommand);
        addCommand(exitCommand);
        addCommand(wapCommand);
        setSelectCommand(goCommand);
        setCommandListener(this);
    }

    public void FileTest() {
        Utility.log("Try to download " + Utility.DOWNLOAD_HOST + 
                Utility.FILE_NAME + " to " + Utility.LOCAL_DIR + Utility.FILE_NAME);
        try {
            SiteInfoBean bean = new SiteInfoBean(Utility.DOWNLOAD_HOST,
                Utility.FILE_NAME, Utility.LOCAL_DIR, Utility.FILE_NAME, Utility.THREAD_NUM);
            //bean.printBean();
            fileFetch = new SiteFileFetch(bean, this);
            fileFetch.start();
        } catch (Exception ex) {
            midlet.showError(ex);
        }
    }
    
    /**
     * The command listener command action implementation.
     * @param c Command
     * @param d Displayable
     */
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
             midlet.fileSelectorExit();
        } else if (c == goCommand) {
            FileTest();
        } else if (c == stopCommand) {
            if (null != fileFetch) fileFetch.siteStop();
        } else if (c == wapCommand) {
            try {
                boolean b = midlet.platformRequest("http://wap.sina.com.cn");
                if (b) {
                    Utility.log("Midlet must exit first before launch.");
                } else {
                    Utility.log("Midlet does NOT need to exit first.");
                }
            } catch (Exception ex) {
                Utility.log(ex.getMessage());
            }
        }
    }
    
    /**
     *
     *
     */
    public void notifyProgress(boolean done, int transferred, int left) {
        String msg = null;
        if (done) {
            msg = "Download finished.";
        } else {
            msg = "Stopped by user, " + transferred + "k transferred and "
                + left + "k left.";
        }
        
        Alert info = new Alert(null, msg, null, null);
        info.setTimeout(4000);
        Display.getDisplay(midlet).setCurrent(info, this);
    } 
    
  /**
   * A method used to display all the roots.
   */
  private void displayAllRoots() {
    setTitle("Media Player - [Roots]");
    deleteAll();
    Enumeration roots = rootsList.elements();
    while (roots.hasMoreElements()) {
      String root = (String) roots.nextElement();
      append(root, null);
    }
    currentRoot = null;
  }

  /**
   * A method used to load the roots.
   */
  private void loadRoots() {
    if (!rootsList.isEmpty()) {
      rootsList.removeAllElements();
    }
    try {
      Enumeration roots = FileSystemRegistry.listRoots();
      while (roots.hasMoreElements()) {
        rootsList.addElement(FILE_SEPARATOR + (String) roots.nextElement());
      }
    }
    catch (Throwable e) {
      midlet.showMsg(e.getMessage());
    }
  }
}

⌨️ 快捷键说明

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