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. 【bzoj4136】[FJOI2015]带子串包含约束LCS问题

    题目描述: 带有子串包含约束的最长公共子序列问题可以具体表述如下. 给定2个长度分别为n和m的序列X和Y,以及一个子串包含约束集S. S中共有k个字符串S={S1,S2,…,Sk},其中字符串Si的长 ...

  2. react-native 组件之间传值

    1.通过 AsyncStorage 将值保存在本地(最低端的方法) import { AsyncStorage, } from 'react-native'; // 保存 IP AsyncStorag ...

  3. sysf接口的函数【转】

    本文转载自:http://blog.csdn.net/manshq163com/article/details/7848714 说道sysfs接口,就不得不提到函数宏 DEVICE_ATTR,原型是 ...

  4. IBM Security App Scan Standard 工具的使用

    1.AppScan是什么? AppScan是IBM的一款web安全扫描工具,可以利用爬虫技术进行网站安全渗透测试,根据网站入口自动对网页链接进行安全扫描,扫描之后会提供扫描报告和修复建议等. AppS ...

  5. 几个FFmpeg 视频参数 fps、tbr、tbn、tbc

    我们用Ffplay播放文件或者视频流命令行会出现fps.tbr.tbn.tbc等参数如下图所示 图1 ffplay 播放文件示意图 fps表示平均帧率,总帧数除以总时长(以s为单位). tbr  表示 ...

  6. 2014 ECML: Covariate-correlated lasso for feature selection (ccLasso)

    今天看了一篇 ECML 14 的文章(如题),记录一下. 原文链接:http://link.springer.com/chapter/10.1007/978-3-662-44848-9_38 这篇文章 ...

  7. tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

    在执行自动化打包的时候报错,检查发现是Xcode的路径被改了 标记3的地方原来默认是没有内容的,点击它,然后会自动弹出一个选项,就是xcode的版本. 修改后,在命令行输入xcodebuild命令测试 ...

  8. MySQL的常用JSON函数

    1. JSON_SEARCH(col ->> '$[*].key', type, val) col: JSON格式的字段名 key:要搜索的col字段的key type:可以为'one'或 ...

  9. docker--docker版本以及安装

    3 docker 版本以及安装 3.1 docker 版本介绍 https://www.docker.com/ Docker CE 在 17.03 版本之前叫 Docker Engine, Docke ...

  10. .Net core 3.0 SignalR+Vue 实现简单的即时通讯/聊天IM (无jq依赖)

    .Net core 中的SignalR JavaScript客户端已经不需要依赖Jquery了 一.服务端 1.nuget安装 Microsoft.AspNetCore.SignalR 2.在star ...