<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    騎豬闖天下

    J2ME隨筆,記錄成長的腳步

    統計

    留言簿(3)

    閱讀排行榜

    評論排行榜

    [J2ME]斷點續傳


    斷點續傳的頭信息設置:
    conn.setRequestProperty("RANGE", "bytes=" + size + "-"); //斷點續傳

    項目中使用斷點續傳,頭信息的屬性按上面設置好了,但無論怎么測試,就是不能實現,費了一天又一個晚上的功夫才查到問題所在,在使用斷點續傳的時候,頭信息的聯網方式設為了post,導致無法續傳!

    實踐證明要實現斷點續傳:聯網方式必須使用get方式,或默認不設置。

    以下是斷點續傳的部分代碼:
    package com.xuudoo.booking;

    import java.io.DataInputStream;
    import java.io.IOException;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.rms.InvalidRecordIDException;
    import javax.microedition.rms.RecordEnumeration;
    import javax.microedition.rms.RecordStoreException;
    import javax.microedition.rms.RecordStoreNotOpenException;

    import com.xuudoo.ewl.EWLScreen;

    public class DownloadTool implements Runnable {
        
    public XItem item;//當前下載的item
        public String imageName;//當前下載的書名
        public int FocusID;//當前下載的ID

        
    public HttpConnection conn = null;
        
    public DataInputStream dis = null;
        
    public RMSRecorderIfno rms;//存放下載的漫畫數據
        public RMSRecorderIfno rms1;//存放下載的url和數據節點個數
        public RMSRecorderIfno rms2; //存放廢字節長度

        
    public DownloadTool(XItem xitem,int focusID) {
            item 
    = xitem;
            imageName 
    = xitem.itemId;
            FocusID 
    = focusID;
        }


        
    public void run() {
            UIManager.isLoading 
    = true;
            
    try {
                System.gc();
                String url 
    = "";
                
    if (item.isResume){
                    downloadFileagain(url);
                }
    else{
                    url 
    = UIManager._currMgz.mgzLocate;
                    downloadFile(url);
                }

                UIManager._instance.doLoop();
                
    do {
                    
    if (UIManager._instance.downloadurl == null)
                        
    break;
                    
    for (int i = 0; i < UIManager._instance.downloadurl.size();) {
                        url 
    = (String) UIManager._instance.downloadurl.elementAt(i);
                        item 
    = (XItem) UIManager._instance.downloaditem.elementAt(i);
                        imageName 
    = item.itemId;
                        FocusID 
    = Integer.parseInt((String) UIManager._instance.downloadid.elementAt(i));
                        
    if (item.isResume)
                            downloadFileagain(url);
                        
    else
                            downloadFile(url);
                        
    break;
                    }

                    UIManager._instance.downloadurl.removeElement(url);
                    UIManager._instance.downloaditem.removeElement(item);
                    UIManager._instance.downloadid.removeElement(Integer.toString(FocusID));
                    UIManager._instance.doLoop();
                }
     while (UIManager._instance.downloadurl != null && UIManager._instance.downloadurl.size() >= 1);
            }
     catch (Exception e) {
                
    if(BookingMidlet.DEBUG)
                    e.printStackTrace();
            }
     finally {
                UIManager.isLoading 
    = false;
                UIManager._instance.doLoop();
            }

        }


        
    public synchronized void StopDownLoadThread() {
            
    if (conn != null{
                notify();
                Close();
            }

        }


        
    public synchronized void CloseMidlet() {
            
    if (dis != null{
                
    try {
                    dis.close();
                }
     catch (IOException e) {
                    e.printStackTrace();
                }

                dis 
    = null;
            }

            
    if (conn != null{
                notify();
                
    try {
                    conn.close();
                }
     catch (IOException e) {
                    e.printStackTrace();
                }

                conn 
    = null;
            }

            
    if (rms != null{
                rms.close();
                rms  
    = null;
            }

            
    if (rms1 != null{
                rms1.close();
                rms1  
    = null;
            }

            
    if (rms2 != null{
                rms2.close();
                rms2  
    = null;
            }

            
    if(UIManager._instance._percent != null)
                UIManager._instance._percent 
    = null;
        }

        
        
    private void Close() {
            
    if (dis != null{
                
    try {
                    dis.close();
                }
     catch (IOException e) {
                    e.printStackTrace();
                }

                dis 
    = null;
            }

            
    if (conn != null{
                
    try {
                    conn.close();
                }
     catch (IOException e) {
                    e.printStackTrace();
                }

                conn 
    = null;
            }

            
    try {
                Thread.sleep(
    500);
                
    if(BookingMidlet.DEBUG)
                    System.out.println(
    "-----關閉連接和RMS.");
            }
     catch (Exception ex) {
                
    //
            }

            
    if (rms != null{
                rms.close();
                rms  
    = null;
            }

            
    if (rms1 != null{
                rms1.close();
                rms1  
    = null;
            }

            
    if (rms2 != null{
                rms2.close();
                rms2  
    = null;
            }

            
    if(UIManager._instance._percent != null)
                UIManager._instance._percent 
    = null;
        }


        
    private int xcblength = 0//整個xcb記錄個數

        
    public void downloadFile(String url){
            
    if (RMSRecorderIfno.exists("002_" + imageName + "_mgz"))
                RMSRecorderIfno.delete(
    "002_" + imageName + "_mgz");
            rms 
    = new RMSRecorderIfno();
            rms.open(
    "002_" + imageName + "_mgz"true);
            
    //
            rms2 = new RMSRecorderIfno();
            
    if (RMSRecorderIfno.exists("002_" + imageName + "_byte")) {
                RMSRecorderIfno.delete(
    "002_" + imageName + "_byte");
            }

            rms2.open(
    "002_" + imageName + "_byte"true);

            rms1 
    = new RMSRecorderIfno();
            rms1.open(
    "002_" + imageName + "_url"true);
            
    byte[] urlbyte = url.getBytes();
            rms1.openWriteStream();
            rms1.write(urlbyte);
            rms1.addRecord(rms1.saveStreamToByteArray());
            rms1.closeWriteStream();
            String precentstr 
    = "0";// 下載進度的百分比
            
            
    try {
                
    //S40_V3版本需通過cmwap才能下載
                if (!BookingMidlet.useProxy){
                    conn 
    = (HttpConnection)Connector.open(url);
                }
     else {
                    String remainUrl 
    = url.substring("http://".length());
                    
    int index = remainUrl.indexOf("/");
                    String path 
    = "";
                    
    if (index != -1{
                        path 
    = remainUrl.substring(index);
                        remainUrl 
    = remainUrl.substring(0, index);
                    }

                    String host 
    = "";
                    String port 
    = "";
                    index 
    = remainUrl.indexOf(":");
                    
    if (index != -1{
                        host 
    = remainUrl.substring(0, index);
                        port 
    = remainUrl.substring(index + 1);
                    }
     else {
                        host 
    = remainUrl;
                        port 
    = null;
                    }

                    String proxyHost 
    = "10.0.0.172";
                    String proxyPort 
    = "80";
                    String cnwapUrl 
    = "http://" + proxyHost + ":" + proxyPort + path;
                    conn 
    = (HttpConnection) Connector.open(cnwapUrl);
                    
    if (port == null)
                        conn.setRequestProperty(
    "X-Online-Host", host);
                    
    else
                        conn.setRequestProperty(
    "X-Online-Host", host + ":" + port);
                }

                dis 
    = conn.openDataInputStream();
                
    // begin analyze db file
                dis.skip(0x50L);//RMS頭 80個字節
                int recNum = dis.readInt();//xcb文件的節點數,4個字節
                rms1.openWriteStream();
                rms1.write(recNum);
                rms1.addRecord(rms1.saveStreamToByteArray());
                rms1.closeWriteStream();
                dis.skip(
    0x1CL);//28個字節的廢字節
                for (int i = 0; i < recNum; i++{
                    
    if (BookingMidlet.limitDownloadSpeed){
                        
    try {
                            Thread.sleep(
    1000);
                        }
     catch (Exception ex) {
                            
    //
                        }

                    }

                    EWLScreen.instance.repaint();
                    
    //16個字節是節點頭
                    dis.readInt();//4個字節的當前節點的序號
                    dis.readInt();//4個字節的廢字節
                    int occupied = dis.readInt();//4個字節表示當前節點所在內存大小
                    int recsize = dis.readInt();//4個字節表示當前節點的實際大小
                    byte recdata[] = new byte[recsize];
                    dis.readFully(recdata);
                    
                    
    if(BookingMidlet.DEBUG)
                        System.out.println(
    "-----save rms data: "+recsize);
                    
    // 以下代碼用以計算下載百分比
                    precentstr = "" + (100 * DownPrecent(rms)) / (recNum + 0.01);
                    precentstr 
    = forMatString(precentstr);
                    UIManager._instance.set_percent(precentstr, FocusID);
                    EWLScreen.prompt2 
    = precentstr;
                    
    ////////////////////////////////////////////////////////////////////////
                    rms.openWriteStream();
                    rms.write(recdata);
                    rms.addRecord(rms.saveStreamToByteArray());
                    rms.closeWriteStream();
                    
                    
    //保存節點后面的廢字節,以便續傳時使用
                    int Wastebytes = occupied - recsize - 16;
                    rms2.openWriteStream();
                    rms2.write(Wastebytes);
                    rms2.addRecord(rms2.saveStreamToByteArray());
                    rms2.closeWriteStream();
                    dis.skip(Wastebytes); 
    // subtract rec header
                }


                UIManager._instance.removeDownloadingItem(item);
                UIManager._instance.openDownloaded();
                UIManager._instance.insertDownloaded(item);
            }
     catch (Exception e) {
                
    if(BookingMidlet.DEBUG){
                    System.out.println(
    "-----中斷下載。");
                }

    //            UIManager.showAlert("下載失敗!請刪除后重新下載");
            }
     finally {
                Close();
            }

        }

        
        
    public void downloadFileagain(String url){
            
    try {
                
    if (RMSRecorderIfno.exists("002_" + imageName + "_mgz"&& !item.isResume)
                    RMSRecorderIfno.delete(
    "002_" + imageName + "_mgz");
                rms 
    = new RMSRecorderIfno();
                rms.open(
    "002_" + imageName + "_mgz"true);
                
    //
                rms2 = new RMSRecorderIfno();
                
    if (RMSRecorderIfno.exists("002_" + imageName + "_byte"&& !item.isResume) {
                    RMSRecorderIfno.delete(
    "002_" + imageName + "_byte");
                }

                rms2.open(
    "002_" + imageName + "_byte"true);
                
                
    //以下代碼用以斷點續傳
                rms1 = new RMSRecorderIfno();
                rms1.open(
    "002_" + imageName + "_url"false);
                url 
    = setValue(rms1);
                
    ////////////////////////////////////////////////////////////////////////////
                String precentstr = "0";// 下載進度的百分比
                precentstr = "" + (100 * DownPrecent(rms)) / (xcblength);
                precentstr 
    = forMatString(precentstr);
                UIManager._instance.set_percent(precentstr, FocusID);
                
    //S40_V3版本需通過cmwap才能下載
                if (!BookingMidlet.useProxy){
                    conn 
    = (HttpConnection)Connector.open(url);
                }
     else {
                    String remainUrl 
    = url.substring("http://".length());
                    
    int index = remainUrl.indexOf("/");
                    String path 
    = "";
                    
    if (index != -1{
                        path 
    = remainUrl.substring(index);
                        remainUrl 
    = remainUrl.substring(0, index);
                    }

                    String host 
    = "";
                    String port 
    = "";
                    index 
    = remainUrl.indexOf(":");
                    
    if (index != -1{
                        host 
    = remainUrl.substring(0, index);
                        port 
    = remainUrl.substring(index + 1);
                    }
     else {
                        host 
    = remainUrl;
                        port 
    = null;
                    }

                    String proxyHost 
    = "10.0.0.172";
                    String proxyPort 
    = "80";
                    String cnwapUrl 
    = "http://" + proxyHost + ":" + proxyPort + path;
                    conn 
    = (HttpConnection) Connector.open(cnwapUrl);
                    
    if (port == null)
                        conn.setRequestProperty(
    "X-Online-Host", host);
                    
    else
                        conn.setRequestProperty(
    "X-Online-Host", host + ":" + port);
                }

                
    //設置Range
                int skipNum = RmsSize(rms, rms2);
                
                
    if (skipNum == 112{
                    dis 
    = conn.openDataInputStream();
                    dis.skip(
    0x50L);//RMS頭 80個字節
                    xcblength = dis.readInt();//xcb文件的節點數,4個字節
                    rms1.openWriteStream();
                    rms1.write(xcblength);
                    rms1.setRecord(
    2, rms1.saveStreamToByteArray());
                    rms1.closeWriteStream();
                    dis.skip(
    0x1CL);//28個字節的廢字節
                }
    else{
                    conn.setRequestProperty(
    "RANGE""bytes=" + skipNum + "-");//斷點續傳
                    dis = conn.openDataInputStream();
                }

                item.isResume 
    = false;
                
    int num = DownPrecent(rms);
                
    for (int i = 0; i < xcblength - num; i++{
                    
    if (BookingMidlet.limitDownloadSpeed){
                        
    try {
                            Thread.sleep(
    1000);
                        }
     catch (Exception ex) {
                            
    //
                        }

                    }

                    EWLScreen.instance.repaint();
                    
    //16個字節是節點頭
                    dis.readInt();//4個字節的當前節點的序號
                    dis.readInt();//4個字節的廢字節
                    int occupied = dis.readInt();//4個字節表示當前節點所在內存大小
                    int recsize = dis.readInt();//4個字節表示當前節點的實際大
                    byte recdata[] = new byte[recsize];
                    dis.readFully(recdata);

                    rms.openWriteStream();
                    rms.write(recdata);
                    rms.addRecord(rms.saveStreamToByteArray());
                    rms.closeWriteStream();

                    precentstr 
    = "" + (100 * DownPrecent(rms)) / (xcblength);
                    precentstr 
    = forMatString(precentstr);
                    UIManager._instance.set_percent(precentstr, FocusID);

                    
    //保存節點后面的廢字節,以便續傳時使用
                    int Wastebytes = occupied - recsize - 16;
                    rms2.openWriteStream();
                    rms2.write(Wastebytes);
                    rms2.addRecord(rms2.saveStreamToByteArray());
                    rms2.closeWriteStream();
                    dis.skip(Wastebytes); 
    // subtract rec header
                }


                UIManager._instance.removeDownloadingItem(item);
                UIManager._instance.openDownloaded();
                UIManager._instance.insertDownloaded(item);
            }
     catch (Exception e) {
                
    if(BookingMidlet.DEBUG){
                    System.out.println(
    "-----中斷下載。");
                }

                
    //UIManager.showAlert("下載失敗!請刪除后重新下載");
            }
     finally {
                Close();
            }


        }


        
    /**
         * 計算百分比
         * 
    @param str
         * 
    @return
         
    */

        
    public static String forMatString(String str) {
            String values;
            
    int index = str.indexOf(".");
            
    if(index != -1){
                values 
    = str.substring(0, index);
            }
    else{
                values 
    = str;
            }

            
    return values;
        }


        
    /**
         *  遍歷RMS,得到當前記錄個數
         * 
    @param rms
         * 
    @return
         
    */

        
    public int DownPrecent(RMSRecorderIfno rms) {
            
    int i = 0;
            
    try {
                RecordEnumeration enu 
    = null;
                
    if (enu == null{
                    enu 
    = rms.rs.enumerateRecords(nullnullfalse);
                }

                
    while (enu.hasNextElement()) {
                    
    try {
                        enu.nextRecordId();
                    }
     catch (InvalidRecordIDException e) {
                        e.printStackTrace();
                    }

                    i
    ++;
                }

            }
     catch (RecordStoreNotOpenException e) {
                e.printStackTrace();
            }

            
    return i;
        }


        
    /**
         *  遍歷RMS,計算當前記錄的長度
         * 
    @param rms
         * 
    @param rms1
         * 
    @return
         
    */

        
    public int RmsSize(RMSRecorderIfno _rms, RMSRecorderIfno _rms2) {
            
    int i = 112;
            
    try {
                RecordEnumeration enu 
    = null;
                
    if (enu == null{
                    enu 
    = _rms.rs.enumerateRecords(nullnullfalse);
                }

                
    while (enu.hasNextElement()) {
                    
    try {
                        i 
    += enu.nextRecord().length;
                        i 
    += 16;
                    }
     catch (InvalidRecordIDException e) {
                        e.printStackTrace();
                    }
     catch (RecordStoreException e) {
                        e.printStackTrace();
                    }

                }

            }
     catch (RecordStoreNotOpenException e) {
                e.printStackTrace();
            }

            i 
    = i + sikpSize(_rms2);
            
    return i;
        }


        
    /**
         *  遍歷RMS,計算rms中各個節點后跳過的節點大小
         * 
    @param rms
         * 
    @return
         
    */

        
    public int sikpSize(RMSRecorderIfno _rms2) {
            
    int len = 0;
            
    try {
                RecordEnumeration enu 
    = null;
                
    if (enu == null{
                    enu 
    = _rms2.rs.enumerateRecords(nullnullfalse);
                }

                
    while (enu.hasNextElement()) {
                    
    try {
                        len 
    += bytesToInt(enu.nextRecord());
                    }
     catch (InvalidRecordIDException e) {
                        e.printStackTrace();
                    }
     catch (RecordStoreException e) {
                        e.printStackTrace();
                    }

                }

            }
     catch (RecordStoreNotOpenException e) {
                e.printStackTrace();
            }

            
            
    return len;
        }


        
    /**
         * byte轉int
         * 
    @param bytes
         * 
    @return
         
    */

        
    public static int bytesToInt(byte[] bytes) {
            
    int result = 0;
            
    for (int i = 0; i < bytes.length; i++{
                result 
    <<= 8;
                result 
    |= (bytes[i] & 0xff); //此處忽略byte的符號位   
            }

            
    return result;
        }

        
        
    private String setValue(RMSRecorderIfno rms){
            String url 
    = "";
            RecordEnumeration enu 
    = null;
            
    if (enu == null{
                
    try {
                    enu 
    = rms.rs.enumerateRecords(nullnullfalse);
                }
     catch (RecordStoreNotOpenException e) {
                    e.printStackTrace();
                }

                
    int i = 0;
                
    while (enu.hasNextElement()) {
                    
    try {
                        i 
    = enu.nextRecordId();
                        
    if (i == 1{
                            url 
    = new String(rms.getRecord(i));
                        }
     else if (i == 2{
                            xcblength 
    = bytesToInt(rms.getRecord(i));
                        }

                    }
     catch (InvalidRecordIDException e) {
                        e.printStackTrace();
                    }

                }

            }

            
    return url;
        }

    }

    posted on 2010-02-25 14:58 騎豬闖天下 閱讀(346) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲成A∨人片在线观看不卡| 国产真实伦在线视频免费观看| 免费观看激色视频网站(性色)| 4455永久在线观免费看| 久久久www成人免费毛片| 永久免费毛片手机版在线看| 免费国产一级特黄久久| 国产亚洲自拍一区| 亚洲av日韩av无码黑人| 亚洲国产精品久久人人爱| 亚洲AV噜噜一区二区三区| 一级a性色生活片久久无少妇一级婬片免费放 | 日韩免费视频播放| 亚洲一区二区三区国产精品| 亚洲中文字幕在线观看| 亚洲网址在线观看| 精品国产_亚洲人成在线| a在线视频免费观看| 亚洲一区二区三区免费观看| 日本高清免费网站| 亚洲一区二区三区AV无码| 亚洲国产成人九九综合| 阿v免费在线观看| 久热免费在线视频| 午夜两性色视频免费网站| 中文字幕人成人乱码亚洲电影| 亚洲性一级理论片在线观看| 看免费毛片天天看| 99热在线免费播放| 国产成人精品高清免费| 亚洲AV日韩AV永久无码绿巨人| 亚洲精品av无码喷奶水糖心| 国产无遮挡裸体免费视频在线观看| 久久久久久久久免费看无码| 亚洲无线观看国产精品| 亚洲人成网站在线在线观看| 久青草视频97国内免费影视| 国产精品无码免费播放| 亚洲AV无码乱码国产麻豆| 亚洲国产精品网站在线播放 | 四虎影视精品永久免费网站|