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. ...
随机推荐
- less使用手记 主题切换 全局import less
实现主题颜色切换 components/theme.less,跟据@theme读取主题布局 @theme: dark; .dark-theme (@transparency) when (@theme ...
- NEW jobs: A Prepare. Prepare. Prepare. 怎么准备都不过分
A Prepare. Prepare. Prepare. From: https://leetcode.com/explore/interview/card/leapai/272/general ...
- 将Eclipse设置为黑色主题
将Eclipse设置为黑色主题 觉得黑色的主题&配色很高大上,于是花了点时间实践出下面一种方法. 修改代码编辑区配色 修改整个软件主题 先上成果图: 但是进度条依旧是白色的,不知道怎么弄了╮( ...
- sleep、yield、wait、join的区别(阿里)
只有runnable到running时才会占用cpu时间片,其他都会出让cpu时间片.线程的资源有不少,但应该包含CPU资源和锁资源这两类.sleep(long mills):让出CPU资源,但是不会 ...
- 重构之字段改名 UML行为图 用例图 时序图&协作图 状态图&活动图 依恋情结
简单的使用一下字段改名 为什么使用字段改名: 你在一个软件上做的工作越多,对这个软件的数据的理解就越深刻,你需要把这些理解融入到代码中.利用名字的解释作用,让代码更容易被理解. 如何找到该变量的所 ...
- abp radio表单元素 消失了
框架将原本的元素都隐藏掉了,取而代之的是根据label定义了自己的样式,如下: [type="radio"]:not(:checked)+label { padding-le ...
- 未能找到 System.Web.Helpers
This question is a bit old but here's a simple solution - Microsoft seemed to just move this library ...
- 用数据让我们的OKR变得“冷酷”却更有价值
在外企工作过的朋友大多能感受到数据在工作中的重要性,你要设置一个目标要有明确的数据来衡量,你要汇报工作进展要有清楚的数据来显示进度,你要展示工作成果更是需要有代表性的数据来呈现结果,数据是真真实实的结 ...
- BZOJ 1802: [Ahoi2009]checker
题目描述 若有两个红格相邻 第一问的答案为0,所有位置上的棋子都可以通过在这两个格子上放棋子得到 第二设f[i]表示想让第i个格子上有棋子需要放的棋子数 若没有,第一问答案为偶数格子上白格的个数,第二 ...
- Problem 2 旅行计划 (travelling .cpp)———2019.10.6
lth tql,lzpclxf tql Orz Problem 2 旅行计划 (travelling.cpp)[题目描述]小 Z 打算趁着暑假,开启他的旅行计划.但与其他同学不同的是,小 Z 旅行时并 ...