FileUtils (从磁盘下载,从网络下载)
public class FileUtils {
/**
* realPath 磁盘路径 D://project/download/
* urlPath 后半部分路径 具体根据业务需求,例如:WEB-INF/download.xlsx
* downLoadName 下载后的新名字
* @param request
* @param resp
*/
public static void downloadFromDisk(HttpServletRequest request, HttpServletResponse resp,String realPath,String urlPath,String downLoadName) {
//文件最终路径,例如:D://project/download/WEB-INF/download.xlsx
String path = realPath + File.separator + urlPath;
File file = new File(path);
if(!file.exists()){
System.out.println("---------<系统找不到指定的文件>---------");
return;
}
resp.reset();
resp.setContentType("application/octet-stream");
resp.setCharacterEncoding("utf-8");
resp.setContentLength((int) file.length());
//下载到本地的文件名,例如:abc.xlsx(注意:这里跟路径中的文件名区分开)
String downloadName = downLoadName;
resp.setHeader("Content-Disposition", "attachment;filename=" + downloadName );
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = resp.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int i = 0;
while ((i = bis.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
System.out.println("---------<文件下载成功>---------");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void downloadFromIntNet(String urlStr, String fileName, String savePath) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为5秒
conn.setConnectTimeout(5*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//创建保存路径
File saveDir = new File(savePath);
if(!saveDir.exists()) {
saveDir.mkdir();
}
//创建文件
File file = new File(saveDir+File.separator+fileName);
//文件输出流
FileOutputStream fout = new FileOutputStream(file);
//输出文件
byte[] buff = new byte[4096];
int len = -1;
while ((len = inputStream.read(buff)) != -1) {
fout.write(buff,0,len);
}
//关闭输出输入流
fout.close();
inputStream.close();
System.out.println("info:"+url+" download success");
}
}
----------------------------------调用---------------------------------------
/*从磁盘下载*/
String realPath = "C:\\files\\images";
String urlPath = "user.png";
String downLoadName = "newname.png";
FileUtils.downloadFromDisk(request,resp,realPath,urlPath,downLoadName);
/*从网络下载*/
String path = "http://yourIP:yourPort/files/201111/images/2019/12/24/2019122409504998593372629015.jpg";
FileUtils.downloadFromIntNet(path,"test.jpg","C:\\myfile\\images");
FileUtils (从磁盘下载,从网络下载)的更多相关文章
- [搜片神器]直接从DHT网络下载BT种子的方法
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr DHT系 ...
- android 图片网络下载github开源框架之Universal-Image-Loader
最近在做妙趣剪纸项目,剪纸应用项目链接.发扬传统文化,大家多多关注. 需要自己搭建服务器,我用的是新浪sae,简直秒杀京东云几条街,把图片放在网上下载,但是图片经常下载要遇到很多问题,包括oom等.所 ...
- WorldWind源码剖析系列:网络下载类WebDownload
网络下载类WebDownload封装了对请求的瓦片进行网络下载的相关操作.该类使用了两个委托类型和一个枚举类型. 该类的类图如下. 网络下载类WebDownload各个字段和属性的含义说明如下: st ...
- 手把手教你写基于C++ Winsock的图片下载的网络爬虫
手把手教你写基于C++ Winsock的图片下载的网络爬虫 先来说一下主要的技术点: 1. 输入起始网址,使用ssacnf函数解析出主机号和路径(仅处理http协议网址) 2. 使用socket套接字 ...
- 网络下载功能实现(downloader ) ---- HTML5+
模块:downloader Downloader模块管理网络文件下载任务,用于从服务器下载各种文件,并支持跨域访问操作.通过plus.downloader获取下载管理对象.Downloader下载使用 ...
- Android 网络下载图片
2中方法: 1. public byte[] downloadResource(Context context, String url) throws ClientProtocolException, ...
- 如何在uboot上实现从网络下载版本镜像并直接在内存中加载之?
这是作者近期项目上遇到的一个需求,描述如下: 一块MT7620N的路由器单板,Flash中已存放一个版本并可以通过uboot正常加载并启动.现在需要:在uboot上电启动过程中,通过外部按键触发干涉, ...
- IntelliJ IDEA 2017.3.5 安装 lombok-plugin-0.17 失败,通过网络下载总是超时
1.问题: IntelliJ IDEA 2017.3.5 安装 lombok-plugin-0.17 失败,通过网络下载总是超时: 2.原因:IntelliJ IDEA 2017.3.5 目前还不支持 ...
- 3.Linux的远程管理及网络下载
3.1 Linux的远程管理 3.1.1 远程管理概述 什么是远程管理: 1.为什么需要远程管理: 服务器通常是Linux系统,而服务器不可能一直在身边,所以就需要远程来操作服务器 企业中通常需要集群 ...
随机推荐
- 测开之路一百四十六:WTForms之表单应用
WTForms主要是两个功能:1.生成HTML标签 2.对数据格式进行验证 官网:https://wtforms.readthedocs.io/en/stable/ 这篇介绍用wtform生成htm ...
- IntToHex
IntToHex是一种函数,功能是将一个值转换成16进制形式的字符串. IntToHex(int Value, int Digits) 来源: 在Delphi.Pascal或C++ Builder中使 ...
- springmvc中获取request对象,加载biz(service)的方法
获取request对象: 首先配置web.xml文件--> <listener> <listener-class> org.springframework.web.con ...
- 跨 Appdomain 对象共享
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 算是立flag吧~~~看明天结果了~~~
嗯...以前做ssh.应该是stratus spring hibernate. 然后现在来了一个新的需求. 要用 java,bootstrap,oracle,spring boot, jquery,m ...
- vim加脚本注释和文本加密
vim /etc/vimrc 一.李导版本 autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()" fun ...
- 8.FTP后门命令执行----Samba命令执行----VMware安装kali----多终端显示
FTP后门命令执行 再次声明,最近听闻不得教受工具使用等言论. 我敢打包票,网络空间安全一级学科的老师和学生是不会说这句话的.未知攻,焉知防. 有工具来检测自己和玩弄它,是幸运的. 犯罪者不会给你提供 ...
- unittest中的断言方法
方法 用途 assertEqual(a,b) a=b assertNotEqual(a,b) a!=b assertTrue(x) x为True assertFa ...
- javascript 异常处理
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- [Web 前端] 028 jQuery 事件
目录 jQuery 的事件 1. 事件绑定 1.1 事件的获取 1.2 基本绑定 1.3 动态绑定 2. 事件触发 2.1 触发的写法 2.2 常用的鼠标事件 3. 事件冒泡和默认行为 3.1 事件冒 ...