• 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>
  • 相关阅读:
    git学习
    小程序强制自动更新
    UI设计规范
    2019前端面试题汇总(vue)
    技术面试笔试题
    阿里云万网虚拟主机安装配置Https(SSL)教程
    [转]Vue项目全局配置微信分享思路详解
    Elasticsearch学习笔记之—分词器 analyzer
    合成图片+合成文字+图片
    C# 在Bitmap上绘制文字出现锯齿的问题
  • 原文地址:https://www.cnblogs.com/xiaogblog/p/11812298.html
Copyright © 2020-2023  润新知