Java读取并下载网络文件的方法
作者:Marydon 时间:2023-03-18 11:47:05
本文实例为大家分享了Java读取并下载网络文件的具体代码,供大家参考,具体内容如下
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Java读取并下载网络文件
* @author Marydon
* @createTime 2017年8月18日下午5:24:09
* @updateTime
* @Email:marydon2017@163.com
* @version:1.0.0
* @referenceLink
* <a href="http://blog.csdn.net/xb12369/article/details/40543649/"> java 从网络Url中下载文件</a>
*/
public class DownloadFromNetwork {
/**
* 从网络Url中下载文件
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdirs();
}
File file = new File(saveDir+File.separator+fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+url+" download success");
}
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static void main(String[] args) {
try{
for (int i = 201; i <=520; i++) {
downLoadFromUrl("https://ss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin/" + i + ".jpg",i + ".jpg","E:/Downloads/wallpaper/baiduSkin");
}
}catch (Exception e) {
// TODO: handle exception
}
}
}
来源:http://www.cnblogs.com/Marydon20170307/p/7402871.html
标签:java,读取,下载,网络文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
遍历Hashtable 的几种方法
2023-07-18 13:49:11
Java中的字符串常量池详细介绍
2023-03-08 09:16:41
java开发之闹钟的实现代码
2021-07-08 12:46:49
![](https://img.aspxhome.com/file/2023/5/74905_0s.png)
dubbo将异常转换成RuntimeException的原因分析 ExceptionFilter
2023-11-24 14:25:13
![](https://img.aspxhome.com/file/2023/3/64143_0s.jpg)
Spring Boot实现分布式系统中的服务发现和注册(最新推荐)
2022-07-10 03:50:23
Android 中SwipeRefreshLayout与ViewPager滑动事件冲突解决方法
2023-02-26 21:34:17
SpringBoot统一返回JSON格式实现方法详解
2021-10-03 20:22:52
C#键值对容器的介绍
2023-04-14 12:26:56
关于各种排列组合java算法实现方法
2023-11-15 05:46:55
Java类加载初始化的过程及顺序
2021-12-09 16:12:46
![](https://img.aspxhome.com/file/2023/8/81928_0s.png)
Android Bitmap像素级操作详解
2022-01-28 15:01:51
![](https://img.aspxhome.com/file/2023/6/125356_0s.png)
C#实现XML文件操作详解
2023-07-16 12:36:52
![](https://img.aspxhome.com/file/2023/0/67050_0s.png)
IntelliJ Plugin 开发之添加第三方jar的示例代码
2021-11-27 13:35:52
![](https://img.aspxhome.com/file/2023/5/77105_0s.png)
Spring实战之Bean定义中的SpEL表达式语言支持操作示例
2021-07-07 10:46:19
Java实现NIO聊天室的示例代码(群聊+私聊)
2023-11-29 00:57:45
![](https://img.aspxhome.com/file/2023/8/59698_0s.jpg)
详解java IO流之缓冲流的使用
2023-08-08 18:33:16
Java 存储模型和共享对象详解
2023-11-16 14:48:46
![](https://img.aspxhome.com/file/2023/7/110817_0s.jpg)
Android Activity向右滑动返回
2021-12-06 16:56:06
![](https://img.aspxhome.com/file/2023/6/138016_0s.jpg)
C#实现文件上传与下载功能实例
2022-11-18 07:59:03
C#算法之散列表
2022-07-30 19:05:52
![](https://img.aspxhome.com/file/2023/7/122677_0s.jpg)