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

📄 filesplitterfetch.java

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

import java.io.*;
import javax.microedition.io.HttpConnection;

public class FileSplitterFetch extends Thread {
    String host = null; // download host
    String sURL = null; // File URL
    long m_nOrgStartPos;
    long nStartPos; // File Snippet Start Position
    long nEndPos; // File Snippet End Position
    int nThreadID; // Thread's ID
    boolean bDownOver = false; // Downing is over
    boolean bStop = false; // Stop identical
    FileAccess fileAccess = null; // File Access interface

    public FileSplitterFetch(String host, String sURL, String sName, long nOrgStart, long nStart, long nEnd,int id) throws IOException 
    {
        this.host = host;
        this.sURL = sURL;
        m_nOrgStartPos = nOrgStart;
        this.nStartPos = nStart;
        this.nEndPos = nEnd;
        nThreadID = id;
        
        Utility.log("Try to download file from [" + host + sURL
            + "] to " + sName + " start:" + nStartPos + " end:"
            + nEndPos + " with thread "+ id);        
        /*
        LogUtils.info("try to download file from [" + host + sURL
                + "] to " + sName  + " orgstart:" + nOrgStart + " start:" + nStartPos + " end:"
                + nEndPos + " with thread "+ id);
        */ 
        fileAccess = new FileAccess(sName, nStartPos - nOrgStart);
    }
    
    public void run()
    {
        Utility.log("FileSplitterFetch runs from start:" + nStartPos + " to:" 
            + nEndPos + " bStop:" + bStop);
        
        HttpConnection httpConn = null;
        InputStream input = null;
        
        try 
        {
            byte[] b = new byte[1024];
            if (nStartPos < nEndPos && !bStop) 
            {
            	Utility.log("Before connection FileSplitterFetch runs from while start:" + nStartPos + " to:" 
                        + nEndPos + " bStop:" + bStop);

                httpConn = (HttpConnection)Utility.createHttpConn(host, sURL);
                httpConn.setRequestMethod(HttpConnection.POST);
                httpConn.setRequestProperty(Utility.PROP_USER_AGENT, Utility.MOBILE_UA);
                String sProperty = "bytes=" + nStartPos + "-";
                httpConn.setRequestProperty("RANGE", sProperty);

            	Utility.log("After connection FileSplitterFetch runs from while start:" + nStartPos + " to:" 
                        + nEndPos + " bStop:" + bStop);
            	
                input = httpConn.openInputStream();
                Utility.log(logResponseHead(httpConn));
                int nRead = (int)(nEndPos - nStartPos);
                
                if( nRead > 1024 )
                	nRead = 1024;
                
            	Utility.log("Before read FileSplitterFetch runs from:" + nStartPos + " to:" + nEndPos);

                while ((nRead = input.read(b, 0 , nRead)) > 0 && nStartPos < nEndPos && !bStop)
                {
                	/*
                	Utility.log("while reading FileSplitterFetch runs from:" + nStartPos + " to:"  + nEndPos);
                	*/
                    nStartPos += fileAccess.write(b, 0, nRead);
                    
                    nRead = (int)(nEndPos - nStartPos);
                    if( nRead > 1024 )
                    	nRead = 1024;
                }
                Utility.log("Thread " + nThreadID + " is over!");
                bDownOver = true;
            }
        } 
        catch (Exception e) 
        {
            Utility.log(e.getMessage());
        } 
        finally 
        {
            try 
            {
                if( httpConn != null )
                	httpConn.close();
                
                if( input != null )
                	input.close();
                
                fileAccess.close();
            }
            catch (Exception ex)
            {
                Utility.log(ex.getMessage());
            }
        }
    }

    /**
     *  Collection http header information
     */
    public String logResponseHead(HttpConnection con) throws Exception {
        StringBuffer ret = new StringBuffer();
        
        for (int i = 1; ; i++) {
            String header = con.getHeaderFieldKey(i);
            if (header != null)
                ret.append(header + " : " + con.getHeaderField(header) + " | ");
            else
                break;
        }
        
        return ret.toString();
    }
    
    public void splitterStop() {
        bStop = true;
    }

    public void splitterStart() {
        bStop = false;
    }
}

⌨️ 快捷键说明

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