DownloadURLFile网络文件下载
|
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import javax.servlet.http.HttpServletResponse; import com.hurong.credit.util.RsaHelper; public class DownloadURLFile { private static final DownloadURLFile downloadURLFile = new DownloadURLFile(); public static DownloadURLFile getInstance() { return downloadURLFile; } /** * 执行下载 * @param rfPosit 远程文件开始下载位置 */ public static void start(long rfPosit,URL remoteFile,File storeFile){ HttpURLConnection httpConn = null; InputStream httpIn = null; try{ System.out.println("打开HttpURLConnection."); httpConn = (HttpURLConnection)remoteFile.openConnection(); httpConn.setRequestProperty("User-Agent","NetFox"); httpConn.setRequestProperty("RANGE","bytes="+rfPosit+"-"); System.out.println("得到HttpInputStream."); httpIn = httpConn.getInputStream(); writeFile(httpIn,storeFile); System.out.println("关闭HttpURLConnection."); httpConn.disconnect(); }catch(Exception ex){ ex.printStackTrace(); } } /** * 从HttpInputStream中读数据并写到本地文件中 * @param in HttpInputStream */ private static void writeFile(InputStream in,File storeFile){ RandomAccessFile fileOut = null; int buffer_len = 512; byte[] buffer = new byte[buffer_len]; int readLen = 0; try{ System.out.println("写本地文件."); fileOut = new RandomAccessFile(storeFile, "rw"); fileOut.seek(fileOut.length()); while(-1 != (readLen = in.read(buffer, 0, buffer_len))){ fileOut.write(buffer, 0, readLen); } fileOut.close(); }catch(Exception ex){ ex.printStackTrace(); } } /** * 大文件下载 * @param path * @param response */ public static void downLoad(String path, HttpServletResponse response){ final int BUFFER_SIZE = 0x3000;// 缓冲区大小为3M boolean isInline = false; // 是否允许直接在浏览器内打开 OutputStream toClient = null; File f=new File(path); /** * * map(FileChannel.MapMode mode,long position, long size) * * mode - 根据是按只读、读取/写入或专用(写入时拷贝)来映射文件,分别为 FileChannel.MapMode 类中所定义的 * READ_ONLY、READ_WRITE 或 PRIVATE 之一 * * position - 文件中的位置,映射区域从此位置开始;必须为非负数 * * size - 要映射的区域大小;必须为非负数且不大于 Integer.MAX_VALUE * * 所以若想读取文件后半部分内容,如例子所写;若想读取文本后1/8内容,需要这样写map(FileChannel.MapMode.READ_ONLY, * f.length()*7/8,f.length()/8) * * 想读取文件所有内容,需要这样写map(FileChannel.MapMode.READ_ONLY, 0,f.length()) * */ MappedByteBuffer inputBuffer=null; long start = 0; try { String filename = new String(f.getName().getBytes(), "ISO8859-1"); String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); response.reset(); String inlineType = isInline ? "inline" : "attachment"; // 是否内联附件 response.setHeader("content-disposition", "attachment;filename=\""+ filename + "\""); //response.setContentType("application/pdf"); inputBuffer = new RandomAccessFile(f, "r").getChannel().map(FileChannel.MapMode.READ_ONLY, 0,f.length()); byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容 start = System.currentTimeMillis(); // if(inputBuffer.capacity()>BUFFER_SIZE){ for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) { if (inputBuffer.capacity() - offset >= BUFFER_SIZE) { for (int i = 0; i < BUFFER_SIZE; i++) dst[i] = inputBuffer.get(offset + i); } else { for (int i = 0; i < inputBuffer.capacity() - offset; i++) dst[i] = inputBuffer.get(offset + i); } toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream;charset=gbk"); toClient.write(dst); } /*}else{ //dst[0]=inputBuffer; toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream;charset=gbk"); toClient.write(inputBuffer.get(0)); }*/ } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != toClient) { toClient.close(); toClient.flush(); } } catch (Exception e) { e.printStackTrace(); } } long end = System.currentTimeMillis(); System.out.println("读取文件文件内容花费:" + (end - start) + "毫秒"); } } |
DownloadURLFile网络文件下载的更多相关文章
- php实现远程网络文件下载到服务器指定目录(方法一)
PHP实现远程网络文件下载到服务器指定目录(方法一) <?php function getFile($url, $save_dir = '', $filename = '', $type = 0 ...
- Linux网络文件下载
wget 以网络下载 maven 包为例 wget -c http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-ma ...
- Android网络文件下载模块整理
一.知识基础 tomcat服务器配置 理解http协议 理解javaIO操作相关知识 SDcard操作知识 Android 权限配置 二.实现步骤 1.从网上获取资源 public String do ...
- JAVA实现网络文件下载
HttpURLConnection conn = null; OutputStream outputStream = null; InputStream inputStream = null; try ...
- php实现远程网络文件下载到服务器指定目录(方法二)
<?php // maximum execution time in seconds set_time_limit (24 * 60 * 60); //if (!isset($_POST['su ...
- java 网络文件下载(并命中文名)
public void download(HttpServletRequest request, HttpServletResponse response){ //获取服务器文件 String fil ...
- [No00006B]方便的网络下载工具wget 可下载网站目录下的所有文件(可下载整个网站)
wget是linux下命令行的下载工具,功能很强大,它能完成某些下载软件所不能做的,比如如果你想下载一个网页目录下的所有文件,如何做呢?网络用户有时候会遇到需要下载一批文件的情况,有时甚至需要把整个网 ...
- JWebFileTrans: 一款可以从网络上下载文件的小程序(一)
一 摘要 JWebFileTrans是一款基于socket的网络文件传输小程序,目前支持从HTTP站点下载文件,后续会增加ftp站点下载.断点续传.多线程下载等功能.其代码已开源到github上面,下 ...
- java 下载网络文件
1.FileUtils.copyURLToFile实现: import java.io.File; import java.net.URL; import org.apache.commons.io. ...
随机推荐
- 如何在macOS下调整磁盘分区大小?
可以在“macOS”下利用磁盘工具并且不抹掉主分区的情况下,随意更改磁盘分区大小的方法.“OS X”经过几次大版本升级以后,也改名为“macOS”,而且系统自带的“磁盘工具”无论是功能和界面也有很大的 ...
- python 监听键盘输入
import sys, select, tty, termios old_attr = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin.fil ...
- AI人工智能-Python实现前后端人机聊天对话
[前言] AI 在人工智能进展的如火如荼的今天,我们如果不尝试去接触新鲜事物,马上就要被世界淘汰啦~ 本文拟使用Python开发语言实现类似于WIndows平台的“小娜”,或者是IOS下的“Siri” ...
- P5021 赛道修建[贪心+二分]
题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 mm 条赛道. C 城一共有 nn 个路口,这些路口编号为 1,2,-,n1,2,-,n,有 n-1n−1 条适合于修建赛道的双向通 ...
- Spring Boot 日志管理
Spring Boot 日志管理 网址 Spring Boot 日志管理 http://blog.didispace.com/springbootlog/ Spring Boot快速入门(四)--日志 ...
- Docker Quick Start
翻译自官方Quick Start: https://hub.docker.com/?overlay=onboarding 以Windows为例 1.下载源码 下载构建第一个容器的所需要的所有的东西 需 ...
- LOJ P10249 weight 题解
每日一题 day58 打卡 Analysis 这道题搜索的想法非常巧妙,从两端向中间找,这样可以保证仅仅对于head或tail而言,需要用到的前缀和与后缀和是单调递增的,这样排个序就解决了. 值得一提 ...
- BZOJ 2600: [Ioi2011]ricehub 双指针+贪心
不难发现,当我们要选的区间确定后,一定会把仓库安排到中间的稻草上(如果是偶数个的话中间两个都行). 然后按照坐标从小到大枚举右指针,左指针一定不递减,双指针扫一下就行了. code: #include ...
- system.stat[resource,<type>]
系统信息. 整型或者浮点型 ent - 该分区有权接收的处理器单元数(float) kthr, - 关于内核线程状态的信息: r - 平均可运行内核线程数(float) b - 虚拟内存管理器等待队列 ...
- webpack资源管理
一.概况 ①webpack不仅可以打包JavaScript模块,甚至它把网页开发中的一切资源的都可以当作模块来打包处理 ②但是webpack本身不支持,它只是一个打包平台,其他资源,例如css.les ...