针对某些场景下,面对服务文件大,或者服务端服务器不稳定时使用该模块。功能代码如下:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class MultiFileDownLoad {
private static int position = 0;
public static void doDownLoad(String url){
File targetFile=new File(MultiFileDownLoad.class.getResource("").getFile());
targetFile=targetFile.getParentFile();
targetFile=targetFile.getParentFile();
targetFile=targetFile.getParentFile();
targetFile=targetFile.getParentFile();
targetFile=new File(targetFile.getAbsolutePath()+File.separator+ System.currentTimeMillis()+".wmv");
if(!targetFile.exists()){
try {
targetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//File targetFile=new File("D://video.wmv");
writeFile(url,targetFile); }
private static InputStream getInputStream(String url,long startPosition){
InputStream inputStream=null;
HttpURLConnection conn=null;
try {
URL filePath=new URL(url);
conn= (HttpURLConnection) filePath.openConnection();
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
long contentLength=conn.getContentLengthLong(); if(startPosition<contentLength){
// 设置断点续传的开始位置
conn.disconnect();
conn=(HttpURLConnection) filePath.openConnection();
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
conn.setRequestProperty("RANGE","bytes="+startPosition);
contentLength=conn.getContentLengthLong();
System.out.println(contentLength);
inputStream = conn.getInputStream();
}else {
return null;
}
}catch (Exception e){
e.printStackTrace();
}/*finally {
conn.disconnect();
}*/
return inputStream;
}
private static void writeFile(String url,File targetFile){
// 数据读写
byte[] buffer = new byte[1024*1024];
int bytesWritten = 0;
int byteCount = 0;
InputStream inputStream =null;
FileOutputStream fos=null;
long fileLength=0;
try {
fos=new FileOutputStream(targetFile,true);
fileLength=targetFile.length();
inputStream = getInputStream(url,fileLength);
while ((byteCount = inputStream.read(buffer)) != -1) {
fos.write(buffer, bytesWritten, byteCount);
}
}catch (Exception e){
//e.printStackTrace();
try {
inputStream.close();
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
writeFile(url,targetFile);
} }
public static void main(String[] args) {
String filepath = "http://10.84.0.6:2880/fmsdrive%2Fvideo%2F%2Fnews%2F2017%2F10%2F10%D4%C28%C8%D5%D3%CD%CC%EF%B9%E3%BD%C7.wmv";
doDownLoad(filepath);
}
}

  

java之大文件断点续传的更多相关文章

  1. java+下载+大文件断点续传

    java两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下: 实现思路:1.服:利用ServerSocket搭建服务器,开启相应端口,进行长连接操 ...

  2. java解决大文件断点续传

    第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname =  ...

  3. java http大文件断点续传上传

    因为需要研究下断点上传的问题.找了很久终于找到一个比较好的项目. 效果: 上传中,显示进度,时间,百分比. 点击[Pause]暂停,点击[Resume]继续. 2,代码分析 项目进行了封装使用最简单的 ...

  4. java+大文件断点续传

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/1269085759/up6-jsp-mysq ...

  5. iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄

    前言:本篇讲解,在前篇iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载的基础上,使用输出流代替文件句柄实现大文件断点续传.    在实际开发中,输入输出流用的比较少,但 ...

  6. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

  7. HTML5 大文件断点续传完整思路整理

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...

  8. B/S大文件断点续传

    一. 功能性需求与非功能性需求 要求操作便利,一次选择多个文件和文件夹进行上传:支持PC端全平台操作系统,Windows,Linux,Mac 支持文件和文件夹的批量下载,断点续传.刷新页面后继续传输. ...

  9. java filechannel大文件的读写

    java读取大文件 超大文件的几种方法 转自:http://wgslucky.blog.163.com/blog/static/97562532201332324639689/   java 读取一个 ...

随机推荐

  1. VBS虚拟键盘十六进制列表

    Set WshShell=WScript.CreateObject("WScript.Shell") '打开我的电脑WshShell.Sendkeys chr(&h88b6 ...

  2. 关于抓取js加载出来的内容抓取

    一.抓取页面 url=https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.ht ...

  3. mysql大数据表删除操作锁表,导致其他线程等待锁超时(Lock wait timeout exceeded; try restarting transaction;)

    背景: 1.有一个定时任务,每10分钟入一批统计数据: 2.另一个定时任务,每天定时清理7天前数据,此定时任务每天01:18:00执行: 现象: 每天01:20:00的统计数据入库失败,异常信息如下, ...

  4. VIRTIO概述和基本原理

    http://smilejay.com/2012/11/virtio-overview/ (KVM连载)5.1.1 VIRTIO概述和基本原理(KVM半虚拟化驱动) 11/15/2012MASTER  ...

  5. (转)Linux 系统性能分析工具图解读(一、二)

    Linux 系统性能分析工具图解读(一.二) 原文:http://oilbeater.com/linux/2014/09/08/linux-performance-tools.html 最近看了 Br ...

  6. 前端性能优化插件 --- PageSpeed Insights

    对于前端工程师来说,前端性能优化始终都是非常重要的一环,它决定了用户体验, 决定了一个用户是否愿意在页面的加载浪费时间, 从而丢失用户. 所以前端性能优化是非常重要的. 下载地址 https://ch ...

  7. D3(v5) in TypeScript 坐标轴之 饼状图生成

    饼状图生成时依旧遇到了类型问题,记录如下: import * as d3 from 'd3'; import * as React from 'react'; class TestGraph exte ...

  8. 浏览器后退->清除原页面div中填写的数据

    需求说明:页面表单用前端用div布局,提交之后跳转到另一个页面,但是考虑到客户奇怪的脑回路,可能会点击浏览器的后退按钮,不知道是个体情况还是都是一样,原本div中填写的数据还依然存在,所以需要让页面在 ...

  9. spring mvc 外键允许Null

    spring mvc 定义模型外键时允许为Null 新手记录,习惯定义模型时将外键的数据类型为long,基本类型. 这样生成的外键是不允许为Null,但是可以通过设置字段的Column注解,使之允许为 ...

  10. [转]NLog Layout Renderers ${}

    https://github.com/nlog/NLog/wiki/Layout-Renderers Layout renderers are template macros that are use ...