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

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. an concreate example

    Step 1: Creating Parts 1. Split the geometry 2. Create the INLET part. 3. Create the OUTLET part. 4. ...

  2. HTML嵌套php

    1.  <?php echo 'if you want to serve XHTML or XML documents, do it like this'; ?> 2.  <scri ...

  3. [转] #!/bin/sh & #!/bin/bash区别

    [From] http://blog.51cto.com/meiling/1826201 在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本.目前研发送测的shell脚本中主要有以下 ...

  4. Oracle之q操作符

    Oracle本身默认的是单引号,但是在大家写存储过程或者写SQL语句时,有时候需要拼SQL或者是SQL的值里需要传入含单引号的值,此时就需要使用两个单引号''''来进行转义,其实oracle本身提供了 ...

  5. archlinux升级firefox的flash插件

    参考:http://blog.csdn.net/kingolie/article/details/53066415 1. 在https://get.adobe.com/flashplayer/下载文件 ...

  6. bugzilla配置邮箱发送邮件问题

    2018-09-25 1.bugzilla注册账号,邮件无法发送 需要登录管理账号,配置邮箱服务 2.Can't locate object method "quit" via p ...

  7. PIE SDK栅格分级渲染

    1.  功能简介 栅格数据分级渲染是根据不同的分级规则,对像元值进行等级划分:并通过对每一级设置不同的显示符号和标注信息,从而达到分级显示的效果. 2.功能实现说明 2.1. 实现思路及原理说明 第一 ...

  8. java se系列(四) 函数、数组、排序算法、二分法、二维数组

    1 函数 1.1  数的概述 发现不断进行加法运算,为了提高代码的复用性,就把该功能独立封装成一段独立的小程序,当下次需要执行加法运算的时候,就可以直接调用这个段小程序即可,那么这种封装形形式的具体表 ...

  9. window.open打开窗口的几种方式

    1. 在当前窗口打开百度,并且使URL地址出现在搜索栏中. window.open("http://www.baidu.com/", "_search"); w ...

  10. jQuery.Flot开发手记

    目录 介绍 使用 自定义参数 自定义图例 自定义坐标 自定义数据序列 自定义网格 其他 鼠标停留在图表节点时显示tooltip 介绍 项目地址:http://www.flotcharts.org/ A ...