java 文件斷點續傳

package Demo;java

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;服務器


public class DownLoad {session

    /**
     * 判斷文件是否存在
     * 
     * @param file
     * @return
     */
    public boolean exist(String file) {
        return new File(file).exists();
    }
    
    /**
     * 忽略證書
     */
    public static void trustEveryone() {
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[] { new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            } }, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }dom

    /**
     * 下載文件
     * @param url
     * @param file
     * @throws IOException
     * @throws InterruptedException 
     */
    public void downFile(String url, String file) throws IOException, InterruptedException {
        if (exist(file)) {
            // 文件存在,則續傳
            long l = getExistsFileLength(file);
            System.out.println("續傳:"+l);
            down(url, l, file);
        } else {
            // 文件不存在,新建
            System.out.println("新文件");
            new File(file).createNewFile();
            down(url, 0, file);
        }
    }url

    /**
     * 獲取本地已存在的文件大小
     * 
     * @param file
     * @return
     */
    public long getExistsFileLength(String file) {
        File f = new File(file);
        return f.length();
    }.net

    /**
     * 下載文件
     * 
     * @param url
     *            地址
     * @param nPos
     *            新文件:0 已存在文件:實際大小
     * @param savePathAndFile
     * @throws InterruptedException 
     */
    @SuppressWarnings("resource")
    public static void down(String url, long nPos, String savePathAndFile) throws InterruptedException  {
        trustEveryone();
        HttpURLConnection conn = null;
        try {
            
            conn = (HttpURLConnection) new URL(url).openConnection();
            //conn.setConnectTimeout(60000);
            //conn.setReadTimeout(60000);
            if(0 != nPos){
                conn.setRequestProperty("RANGE", "bytes="+nPos+"-");                
            }
            // 得到輸入流
            try{
                InputStream input = conn.getInputStream();                
                String serverSize = conn.getHeaderField("Content-Length");
                if(Long.valueOf(serverSize) == nPos){
                    // 本地文件和服務器文件大小同樣
                    System.out.println("本地文件是最新");
                }else{
                    RandomAccessFile oSavedFile = new RandomAccessFile(savePathAndFile, "rw");
                    // 定位文件指針到nPos位置
                    oSavedFile.seek(nPos);
                    byte[] b = new byte[1024];
                    int nRead;
                    // 從輸入流中讀入字節流,而後寫到文件中
                    while ((nRead = input.read(b, 0, 1024)) > 0) {
                        (oSavedFile).write(b, 0, nRead);
                    }
                }
            }catch(IOException e){
                System.out.println("本地文件是最新");
            }
            conn.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
 指針

相關文章
相關標籤/搜索