當前位置 主頁 > 網站技術 > 代碼類 > 最大化 縮小

    Java通過URL獲取公眾號文章生成HTML的方法(2)

    欄目:代碼類 時間:2019-12-31 12:07

    DownLoadImg類

    package com.iueang.util;
     
    import java.io.ByteArrayOutputStream;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.UUID;
     
    import sun.misc.BASE64Encoder;
    public class DownLoadImg {
     public static String downloadPicture(String urlList) {
      String filename="iueang"+UUID.randomUUID().toString()+".png";
      String path="D:/m2/"+filename;
         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);
           fileOutputStream.write(output.toByteArray());
           dataInputStream.close();
           fileOutputStream.close();
         } catch (MalformedURLException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }
         System.out.println("Download返回的filname="+filename);
     return filename;
       }
    }
     
    

    以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持IIS7站長之家。

青海十一选五开奖数据