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. ...
随机推荐
- Httpd服务进阶知识-LAMP源码编译安装
Httpd服务进阶知识-LAMP源码编译安装 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必大家都知道,动态资源交给fastcgi程序处理,静态资源依旧由httpd服务器处理 ...
- 目标检测论文解读4——Faster R-CNN
背景 Fast R-CNN中的region proposal阶段所采用的SS算法成为了检测网络的速度瓶颈,本文是在Fast R-CNN基础上采用RPN(Region Proposal Networks ...
- C#程序保存dump文件
作用 程序异常崩溃前使用此类为进程创建DUMP文件,之后可以使用WinDbg等工具进行分析. 辅助类代码 using System; using System.Diagnostics; using S ...
- 牛客NOIP暑期七天营-提高组2C:滑块(平衡树) (这里rope骗分)
A:hash 或者 map 或者trie. #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) usin ...
- 笨方法学Python3(21-44)
相关代码详见github地址:https://github.com/BMDACMER/Learn-Python 接着前天的总结 习题21:函数可以返回某些东西 定义函数的加减乘除,以及嵌套使用 习题2 ...
- 例程使用(1-4)共享内存 存图片+vector容器教程
1传输的数据 1-1数据格式说明 1 两路视频图像Mat 图像 图像数据(Mat)+图像头信息(ImgInf) //图像的宽.高.类型信息 typedef struct { int width; // ...
- Python -- 正则表达式 regular expression
正则表达式(regular expression) 根据其英文翻译,re模块 作用:用来匹配字符串. 在Python中,正则表达式是特殊的字符序列,检查一个字符串是否与某种模式匹配. 设计思想:用一 ...
- NOI2019 Day1游记
Day1挂了,没什么好说的... 开场T1想到70分暴力就走人了,后来发现可以写到85...(听说有人写dfs过了95?233333) T2刚了2个多小时,得到每次只在中间填最大值的结论后不会区间DP ...
- 洛谷P1979华容道
题目 此题目中存在三种棋盘的放置方法(空白,不能活动,能活动). 而每次变化的格子一定在当前空白格子的周围,因此只需要对空白格子的周围四个状态考虑即可,因此我们设\(a[i][j][k]\)为白格子在 ...
- SSL证书创建与部署
SSL证书简介SSL证书创建SSL证书部署-NginxSSL证书部署-ApacheSSL证书部署-Tomcat SSL简介以及发展SSL协议原理SSL应用场景 SSL简介以及发展传输层安全性协议,以及 ...