java通过图片URL下载图片
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下载图片的更多相关文章
- C++根据图片url下载图片
需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...
- 从url下载图片--java与python实现方式比较
从url下载图片--java与python实现方式比较 博客分类: 技术笔记小点滴 javapython图片下载 一.java的实现方式 首先读取图片 //方式一:直接根据url读取图片 priva ...
- JAVA 通过url下载图片保存到本地
//java 通过url下载图片保存到本地 public static void download(String urlString, int i) throws Exception { // 构造U ...
- java从网络中下载图片到本地
public class imageDownload { public static void main(String[] args) { String url = "http://loca ...
- Java学习笔记——IO操作之以图片地址下载图片
以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...
- 根据url下载图片和页面
需要将&tp=webp&wxfrom=5去掉,既可以在任何地方显示,也可以下载了 http://mmbiz.qpic.cn/mmbiz_jpg/bf8pC39RBhGFOH1ib9Ac ...
- 【转】java URLConnection从网上下载图片或音乐
try { //根据String形式创建一个URL对象, URL url = new URL("http://www.baidu.com"); //实列一个URLconne ...
- IOS开发-UI学习-根据URL显示图片,下载图片的练习(button,textfield,image view,url,data)
编写一个如下界面,实现: 1.在文本输入框中输入一个网址,然后点击显示图片,图片显示到UIImageView中. 2.点击下载,这张显示的图片被下载到手机的Documents文件夹下的Dowmload ...
- QT通过url下载图片到本地
/* strUrl:下载图片时需要的url strFilePath:下载图片的位置(/home/XXX/YYY.png) */ void ThorPromote::downloadFileFromUr ...
随机推荐
- window 连接服务器工具
Xshell xftp 下载网址 以上两个软件均免费, 只需要邮件激活即可. 其中 xshell 主要用来连接服务器,方便使用命令行.xftp 方便传输文件.
- EF 查询时,返回其中一张表(省掉一个个写字段的麻烦)
1.使用partial将需要添加的字段进行扩展 public partial class T_OrderInfo { public string EntName { get; set; } } 2.使 ...
- Windows Server 2003 IIS 使用 Excel.Application
在Server2003服务器系统中,配置ASP调用CreateObject("Excel.Application")对象 1.先在服务器中安装Microsoft Excel 2.打 ...
- bzoj 4298 [ONTAK2015]Bajtocja——哈希+启发式合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4298 题面: 给定d张无向图,每张图都有n个点.一开始,在任何一张图中都没有任何边.接下来有 ...
- [IOI2008] Fish 鱼
https://www.luogu.org/recordnew/lists?uid=56840 题解 首先可以发现我们对于每种颜色的鱼,长一点的能够覆盖的方案已定完全包含短一点的方案. 所以我们可以只 ...
- flask中的Configuration为何这样写
flask中的Configuration flask中,我们需要用到很多配置.我们知道最简单的flask是: from flask import Flask app = Flask(__name__) ...
- leetcode-mid-array-73 set matrix zeros
mycode 空间复杂度 m+n 思路:用set把为0元素所在行.列记录下来 注意:注释的方法更快 class Solution(object): def setZeroes(self, matrix ...
- Idea导入项目详解
1.点击Import Project选择项目所在目录 2.Import Project选项区别: 1)如果选择Create project from existing sources选项, 则你只能导 ...
- 职位-CEO:CEO
ylbtech-职位-CEO:CEO 首席执行官(Chief Executive Officer,缩写CEO),职位名称,是在一个企业中负责日常事务的最高行政官员,主司企业行政事务,又称作司政.行政总 ...
- 包管理器 - peer dependency 的安装
npm 和 yarn 安装依赖(包)时不会自动安装 peer dependence(虽然很旧的 npm 是会自动安装的,但几乎没人用那么旧的了),而是给出如下警告: $ npm install --s ...