zoukankan      html  css  js  c++  java
  • java根据图片的url地址下载图片到本地

    package com.daojia.haobo.aicircle.util;
    import sun.misc.BASE64Encoder;
    
    import java.io.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    
        public class DownloadPicFromUrl {
            public static void main(String[] args) {
                String url = "http://XXXXXX33ayDdEs%3D";
                String path="c:/test/pic.jpg";
                downloadPicture(url,path);
            }
            //链接url下载图片
            private static void downloadPicture(String urlList,String path) {
                URL url = null;
                try {
                    url = new URL(urlList);
                    DataInputStream dataInputStream = new DataInputStream(url.openStream());
    
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
    
                    byte[] buffer = new byte[1024];
                    int length;
    
                    while ((length = dataInputStream.read(buffer)) > 0) {
                        output.write(buffer, 0, length);
                    }
                    BASE64Encoder encoder = new BASE64Encoder();
                    String encode = encoder.encode(buffer);//返回Base64编码过的字节数组字符串
                    System.out.println(encode);
                    fileOutputStream.write(output.toByteArray());
                    dataInputStream.close();
                    fileOutputStream.close();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
  • 相关阅读:
    input框限制只能输入正整数、字母、小数、
    css水平垂直居中
    Android开发之旅-获取地理位置的经度和纬度
    Android DDMS应用
    Android开发BUG及解决方法2
    Android开发BUG及解决方法1
    Android开发BUG及解决方法
    Android系统架构
    1.sql简介
    C语言笔试常考知识点
  • 原文地址:https://www.cnblogs.com/cuixiaomeng/p/10109335.html
Copyright © 2011-2022 走看看