日期:2014-05-20  浏览次数:20753 次

J2ME断点下载文件
package downfile;

import java.io.*;

import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2009</p>
*
* <p>Company: </p>
*
* @author new soul
* @version 1.0
*/
public class Load extends Thread {
    private String url;//网络文件的地址
    private String path;//本地保存路径
    private long loadsize=0;//下载文件的大小
    private long size=0;//已下载的文件大小
    private MIDlet midlet;

    /**
     * 下载线程
     * @param urlstr String 网络文件地址
     * @param pathstr String 本地存储路径
     * @param mid MIDlet 当前显示主类
     * 手机地址为 file:///E:/     电脑模拟器地址 file://localhost/root1/
     */
    public Load(String urlstr,String pathstr,MIDlet mid) {
        this.url=urlstr;
        this.path=pathstr;
        this.midlet=mid;
        Thread thread=new Thread(this);
        thread.start();
    }

    public void run() {
        try {
            LoadFile(url, path);
            Alert alert=new Alert("下载提示","下载已完成!",null,AlertType.ALARM);
            alert.setTimeout(3000);
            Display.getDisplay(midlet).setCurrent(alert);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 文件的断点下载
     * @param urladress String 网络文件的地址
     * @param pathstr String 文件存储的本地地址
     */
    public void LoadFile(String urladress, String pathstr) {
        HttpConnection conn = null;
        FileConnection fc = null;
        InputStream is = null;
        OutputStream os = null;

        //截取文件名
        String[] urlstr = Tool.Split(urladress, "/");
        String filename = urlstr[urlstr.length - 1].toString();

        try {
            conn = (HttpConnection) Connector.open(url.trim());
            conn.setRequestMethod(HttpConnection.POST);

            //手机地址为 file:///E:/     电脑地址file://localhost/root1/
            fc = (FileConnection) Connector.open(pathstr + filename,Connector.READ_WRITE);
            if (fc.exists())
                this.size = fc.fileSize();
            conn.setRequestProperty("RANGE", "bytes=" + size + "-"); //断点续传
            setLoadsize(conn.getLength());//下载文件长度

            is = conn.openInputStream();
            int ch;
            byte[] b = new byte[1024];

            if (!fc.exists()) {//文件不存在
                fc.create();
                os = fc.openOutputStream();
            }

            else {//文件已经存在
                fc.truncate(size);
                os = fc.openOutputStream(size);
            }

            while ((ch = is.read(b)) != -1) {
                    os.write(b, 0, ch);
                }

            fc.close();
            os.close();
            is.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setLoadsize(long loadsize) {
        this.loadsize = loadsize;
    }

    public long getLoadsize() {
        return loadsize;
    }

    public long getSize() {
        return size;
    }

}




本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/laigb/archive/2009/03/15/3992367.aspx