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. ...
随机推荐
- linux定时任务crontab怎样执行root命令
在/var/spool/cron/文件夹下放置了所有定时任务文件, 1.运行crontab -u $用户名 -e会显示$用户的所有定时任务: 2.运行sudo crontab -e会显示root用户所 ...
- linux中的halt和shutdown
最近一直在学鸟哥的Linux私房菜,突然想起来,halt 和 shutdown的区别好像没有搞懂 ** 所以我检索了一下,发现区别是这样的.halt和shutdown都是关闭系统,但是halt不会关闭 ...
- cmds系统归并缓慢的处理过程 2017-2-16
检查反应较慢的时间段,数据库整体运行情况,从awr报告总看看是否有异常 以上是两个节点的等待事件排行,其中发现cursor: pin S wait on X等待事件类型是并行,切较高,开始分析 ...
- 如何获取gitee上的项目?
对于没有使用过github/gitee的朋友来说,估计是有点懵. 下面举个例子,比如获取我的gitee上的python接口自动化测试框架 访问主页:https://gitee.com/uncleyon ...
- 【大数据】HBase启动
zkServer.sh start start-dfs.sh start-yarn.sh start-hbase.sh hbase shell exit http://192.168.1.180:16 ...
- Hbase扩展
1 HBase在商业项目中的能力 每天: 1) 消息量:发送和接收的消息数超过60亿 2) 将近1000亿条数据的读写 3) 高峰期每秒150万左右操作 4) 整体读取数据占有约55%,写入占有45% ...
- 不要轻易在java ext 目录放任何三方jar包
今天在编写一个简单spi 应用demo的时候,在编译时总有一个其他的错误,如下: ERROR Failed to execute goal org.apache.maven.plugins:maven ...
- ESA2GJK1DH1K微信小程序篇: 安装Nginx,配置反向代理
前言 一,为什么需要反向代理 小程序访问的是 443端口,咱需要把443端口的数据传给MQTT 这节为了避免大家配置出错,以下源码已经配置. 如果大家想自己配置,请参考 https://www.cnb ...
- ES5对象新增的方法
Object.is()-----判断是否全等,和===一样Object.assign()-----对象的复制Object.keys()-----以数组的形式返回所有可遍历属性的键名,原型上的属性不能遍 ...
- orm功能封装
封装功能: 查 : select **kwargs.keys() --返回-> obj -转为-->list [{},{}] ---> [obj,obj] class Models( ...