根据网络地址把图片保存到本地

package com.future.test;

import com.baoqilai.scp.exception.TipsException;
import com.baoqilai.scp.util.ExcelUtil;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImgItemAddressTest {

    public static void main(String[] args) throws Exception{

        String TEMPLATE_PATH = "C:/Users/Administrator/Desktop/商品信息/itemimageaddress.xlsx";
        hale(TEMPLATE_PATH);
    }

    private static void hale(String path) {
        try {
            Resource resource =  new FileSystemResource(path);
            List<Map<Integer, Object>> itemList= ExcelUtil.parseExcel2(resource.getInputStream());
            if (itemList==null||itemList.size()==0) throw new TipsException("文件解析为空");
            Map<Integer,List<Map<Integer, Object>>> typeMap=new HashMap();

            Map<Integer,String> nameMap=new HashMap<>();
            for (int i = 0; i <itemList.size() ; i++) {
                Map<Integer, Object> map=itemList.get(i);
                Integer type=Integer.valueOf(map.get(3).toString());
                String typeName=map.get(4).toString();
                List<Map<Integer, Object>> mapList=new ArrayList<>();
                if (!typeMap.isEmpty()&&typeMap.containsKey(type)){
                    mapList=typeMap.get(type);
                }else{
                    nameMap.put(type,typeName);
                }
                mapList.add(map);
                typeMap.put(type,mapList);
            }
            //创建目录
            mkdirF(nameMap);

            for(Map.Entry<Integer, List<Map<Integer, Object>>> entry : typeMap.entrySet()){
                List<Map<Integer, Object>> list = entry.getValue();
                String typeName=list.get(0).get(4).toString();
                //创建目录
                String dirName = "C:/Users/Administrator/Desktop/商品信息/item/"+typeName;

                System.out.println("====1" + dirName );
                for (int i = 0; i < list.size(); i++) {
                    String barCode=list.get(i).get(0).toString();
                    String address=list.get(i).get(5).toString();
                    String patch=dirName+"/"+barCode+".jpg";
                    System.out.println("====2" + patch );
                    downloadPicture(address,patch);
                }
            }
        }catch (Exception e){

        }
    }

    public static void mkdirF(Map<Integer,String> nameMap){
        for(Map.Entry<Integer,String> entry : nameMap.entrySet()){
            String dirName = "C:/Users/Administrator/Desktop/商品信息/item/"+entry.getValue();
            createDir(dirName);
        }
    }

    //链接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);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            System.out.println("创建目录" + destDirName + "成功!");
            return true;
        } else {
            System.out.println("创建目录" + destDirName + "失败!");
            return false;
        }
    }
}

根据网络地址把图片保存到本地

参考 

https://blog.csdn.net/qq_24521431/article/details/104466583