java list断点续传_java 文件断点续传

package Demo;

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 {

/**

* 判断文件是否存在

*

* @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();

}

}

/**

* 下载文件

* @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);

}

}

/**

* 获取本地已存在的文件大小

*

* @param file

* @return

*/

public long getExistsFileLength(String file) {

File f = new File(file);

return f.length();

}

/**

* 下载文件

*

* @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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值