public InputStream getInputStream(String imgUrl) {
InputStream inputStream = null;
try{
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(imgUrl).openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");
httpURLConnection.setRequestProperty("Accept-Encoding", "gzip");
httpURLConnection.setRequestProperty("Referer","no-referrer");
httpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setReadTimeout(20000);
inputStream = httpURLConnection.getInputStream();
}catch (IOException e){
e.printStackTrace();
}
return inputStream;
} public boolean downloadImg(InputStream inputStream,String path){
boolean flag = true;
File file = new File(path);
if (file.exists()){
return flag;
}
File fileParent = file.getParentFile();
if (!fileParent.exists()){
fileParent.mkdirs();//创建路径
}
try {
FileUtils.copyToFile(inputStream,file);
}catch (Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

java通过图片URL下载图片的更多相关文章

  1. C++根据图片url下载图片

    需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...

  2. 从url下载图片--java与python实现方式比较

    从url下载图片--java与python实现方式比较 博客分类: 技术笔记小点滴 javapython图片下载  一.java的实现方式 首先读取图片 //方式一:直接根据url读取图片 priva ...

  3. JAVA 通过url下载图片保存到本地

    //java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...

  4. java从网络中下载图片到本地

    public class imageDownload { public static void main(String[] args) { String url = "http://loca ...

  5. Java学习笔记——IO操作之以图片地址下载图片

    以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...

  6. 根据url下载图片和页面

    需要将&tp=webp&wxfrom=5去掉,既可以在任何地方显示,也可以下载了 http://mmbiz.qpic.cn/mmbiz_jpg/bf8pC39RBhGFOH1ib9Ac ...

  7. 【转】java URLConnection从网上下载图片或音乐

    try { //根据String形式创建一个URL对象,   URL url = new URL("http://www.baidu.com");   //实列一个URLconne ...

  8. IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)

    编写一个如下界面,实现: 1.在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中. 2.点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload ...

  9. QT通过url下载图片到本地

    /* strUrl:下载图片时需要的url strFilePath:下载图片的位置(/home/XXX/YYY.png) */ void ThorPromote::downloadFileFromUr ...

随机推荐

  1. 关联规则挖掘--Eclat算法

  2. three arrays

    three arrays 字典树上贪心 #include<bits/stdc++.h> using namespace std; ][][]; ][]; ]; ]; ][]; ][]; i ...

  3. instanceof用来判断啥?

    java中的instanceof运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例.

  4. C++中一些容易迷惑的语法点总结

    #include<iostream> #include<cstring> using namespace std; int main(){ ][]={{,,},{,,}}; ] ...

  5. multipages-generator今日发布?!妈妈再也不用担心移动端h5网站搭建了!

    本文适合的读者?‍?‍?‍? 现在在手淘,京东,今日头条,美柚等过亿用户的手机app中的,都常见h5网页,他们有更新快,灵活,便于分享和传播的特性.这里有他们中的几个h5的例子:(手淘,美柚).这些a ...

  6. flex几种多列布局

    基本的等分三列布局 .container{ display: flex; width: 500px; height: 200px; } .left{ flex:1; background: red; ...

  7. Java 实现 对象和转字符串之间的互转 (json格式)

    添加依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> ...

  8. win7 编译postgresql9.6.8

    一.环境 windows7 postgresql9.6.8 vs2010 perl5.24.3 二.编译安装 1.安装perl,安装到C:\Perl64路径下,安装完成后设置环境变量PATH和Perl ...

  9. 解决 ffmpeg 在avformat_find_stream_info执行时间太长

    用ffmpeg做demux,网上很多参考文章.对于网络流,avformt_find_stream_info()函数默认需要花费较长的时间进行流格式探测,那么,如何减少探测时间内? 可以通过设置AVFo ...

  10. firefox的group群组功能很好!

    ================================ /usr/share/themes中有一些主题,包括: adwaita: 阿德维塔, 不二论 anaconda等等. 计算机环境: f ...