zoukankan      html  css  js  c++  java
  • 从网络上获取图片,并写入excel文件

    package com.weChat.utils;

    import com.manage.utils.DateUtil;
    import com.manage.utils.MD5Util;
    import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
    import org.apache.poi.hssf.usermodel.HSSFPatriarch;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.wicket.common.utils.DateUtils;

    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class UrlImg {

    /**
    * @param url 网络图片地址
    * @param filePath 保存图片的父级文件夹路径
    * @return
    */
    public String downloadImgByUrl(String url, String filePath) {
    FileOutputStream fos = null;
    /* BufferedInputStream bis = null;*/
    HttpURLConnection httpUrl = null;
    URL netUrl = null;
    String fileName = "";
    try {
    netUrl = new URL(url);
    httpUrl = (HttpURLConnection) netUrl.openConnection();
    httpUrl.connect();
    //bis = new BufferedInputStream(httpUrl.getInputStream());

    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

    BufferedImage bufferImg = ImageIO.read(httpUrl.getInputStream());
    ImageIO.write(bufferImg, "jpg", byteArrayOut);

    int width = bufferImg.getWidth();//原始宽度
    int height = bufferImg.getHeight();//原始高度

    System.out.println("图片的高度:" + height + " 宽度:" + width);


    HSSFWorkbook workbook = new HSSFWorkbook();
    // 生成一个表格
    HSSFSheet sheet = workbook.createSheet("图片导出测试");
    // 设置A列的宽度为30*256;
    sheet.setColumnWidth(0, 200 * 256);
    Row row = sheet.createRow(0);
    row.setHeight((short) (height / 2 * 100));

    // 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    // anchor主要用于设置图片的属性
    HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0, (short) 6, 16 );
    anchor.setAnchorType(3);
    // 插入图片
    patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));


    String time = DateUtils.getTimeRandom("yyMMddhhmmss");
    fileName = MD5Util.MD5(time) + ".xls";//图片的类型,我默认设定为jpg格式;可以自定义文件类型的,网络图片地址应该会有图片类型的,这里就需要你自己去看一下网络图片地址的规则了


    filePath = filePath + "/" + fileName;
    File outFile = new File(filePath);
    outFile.getParentFile().mkdirs();
    if (!outFile.exists()) {
    outFile.createNewFile();
    }
    fos = new FileOutputStream(outFile);
    // 写入excel文件
    workbook.write(fos);

    fos.close();
    /* byte[] buffer = new byte[3042];
    int bytesRead = 0;

    while ((bytesRead = workbook.read!= -1) {
    fos.write(buffer, 0, bytesRead);
    }*/




    } catch (Exception e) {

    e.getMessage();
    //System.out.print("请确认网络图片是否正确!");
    }
    return fileName;
    }

    public static void main(String[] args) {

    new UrlImg().downloadImgByUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523422575372&di=1ae9823657534b984bce8f3d36b579a1&imgtype=0&src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Farchive%2F4516522608648333f98851fb66fcc5432eee9faa.jpg", "D:\二维码");
    }
    }
  • 相关阅读:
    298. Binary Tree Longest Consecutive Sequence
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    163. Missing Ranges
    336. Palindrome Pairs
    727. Minimum Window Subsequence
    211. Add and Search Word
    年底购物狂欢,移动支付安全不容忽视
    成为程序员前需要做的10件事
    全球首推iOS应用防破解技术!
  • 原文地址:https://www.cnblogs.com/git-niu/p/8796545.html
Copyright © 2011-2022 走看看