Java下载HTTP URL链接示例
2024-09-01 12:51:18
这里以下载迅雷U享版为例。
示例代码:
package com.zifeiy.snowflake.handle.filesget;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class HttpFileGetter {
public boolean download(String urlString, File localFile) {
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5*1000);
InputStream inputStream = connection.getInputStream();
byte[] byteArr = new byte[1024];
int len;
File dir = localFile.getParentFile();
if (dir.exists() == false)
dir.mkdirs();
OutputStream outputStream = new FileOutputStream(localFile);
while ((len = inputStream.read(byteArr)) != -1) {
outputStream.write(byteArr, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
// main for test
public static void main(String[] args) {
HttpFileGetter httpFileGetter = new HttpFileGetter();
httpFileGetter.download("http://down.sandai.net/ThunderVIP/ThunderVIP-xlgw.exe", new File("D:\\test_download.exe"));
}
}
下载下来的程序运行也没有问题:
最新文章
- 用FineReport做的共建共享填报系统
- java 读取数据库中表定义
- iOS应用数据存储2-SQLite3数据库
- 【crunch bang】 tint2-用来控制桌面的布局
- ListBox重绘
- ";Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list.xml,reason: Connection
- C# 面向对象 , 类与对象
- graph driver-device mapper-01driver初始化
- API变了,客户端怎么办?
- thinkphp5自动完成
- PowerCLI: One-Liner to get VMs, Clusters, ESX Hosts and Datastores并导入数据库中
- ASP.NET MVC页面报错System.InvalidOperationException The view found at '~/Views/Home/Index.cshtml' was not created.
- 洛谷P1582 倒水题解
- Web QQ 协议 登录加密算法 —— VC++实现
- python基础一 ------可迭代类型的连接
- cto职责
- PMS-授权中心
- 13,EasyNetQ-错误条件
- oracle基础 管理索引
- 3. sklearn的K-Means的使用
热门文章
- Airtest---UI自动化测试项目
- Spark RDD 到 LabelPoint的转换(包含构造临时数据的方法)
- [Javascript] Construct a Regex to Match Twitter Mentions with Regexr
- boost::singleton
- MySql存储过程参考
- PostgreSQL 多版本的实现与Innodb和oracle的差别
- [luogu] zpl的数学题1
- 【原创】go语言学习(八)切片
- linux 查看内存条详情命令
- 牛客训练21674——牛牛与LCM