• 读取网络图片url,并打包生成zip,上传到服务器某个目录


    package com.yft.util;

    import lombok.extern.slf4j.Slf4j;

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;

    /**
    * @Author wjx
    * @Date 2022/3/16
    */
    @Slf4j
    public class FileZipUtils {
    public static void main(String[] args) throws Exception {
    String img1 = "网络图片url";
    String img2 = "网络图片url";
    String fileStr = "D:\\image\\";


    for (int i = 0; i < 2; i++) {
    String zipFileStr = "D:\\imageZip\\压缩包" + i + ".zip";

    List<Map<String, String>> imageUrls = new ArrayList<>();
    Map<String, String> imageUrl = new HashMap<>();
    imageUrl.put("图片1", img1);
    imageUrl.put("图片2", img2);
    imageUrls.add(imageUrl);
    FileZipUtils.FileMain(imageUrls,fileStr,zipFileStr);
    }

    //

    }

    public static void FileMain(List<Map<String, String>> imageUrls,String fileStr,String zipFileStr) throws Exception{
    try {
    // 创建一个文件夹
    File file = new File(fileStr);
    file.mkdirs();
    //多个图片下载地址
    for(int i=0;i<imageUrls.size();i++) {
    Map<String, String> img = imageUrls.get(i);
    for (Map.Entry<String, String> stringStringEntry : img.entrySet()) {
    String fileName = stringStringEntry.getKey();
    String fileUrl = stringStringEntry.getValue();
    //new一个URL对象
    URL url = new URL(fileUrl);
    //打开链接
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    //设置请求方式为"GET"
    conn.setRequestMethod("GET");
    //超时响应时间为10秒
    conn.setConnectTimeout(10 * 1000);
    //通过输入流获取图片数据
    InputStream inStream = conn.getInputStream();
    //得到图片的二进制数据,以二进制封装得到数据,具有通用性
    byte[] data = readInputStream(inStream);
    //new一个文件对象用来保存图片,默认保存当前工程根目录
    File imageFile = new File(fileStr + fileName + ".png");
    //创建输出流
    FileOutputStream outStream = new FileOutputStream(imageFile);
    //写入数据
    outStream.write(data);
    //关闭输出流
    outStream.close();
    }

    }

    // 压缩法
    FileOutputStream fos1 = new FileOutputStream(new File(zipFileStr));
    FileZipUtils.toZip1(fileStr, fos1,false);

    // 删除文件和压缩文件
    FileZipUtils.delFolder(fileStr);
    //FileUtil.delFolder(zipFileStr);

    } catch (MalformedURLException e) {
    log.warn("图片url格式错误:", e);
    } catch (IOException e) {
    log.warn("图片没找到:", e);
    }
    }

    /**
    * 递归压缩方法
    * @param sourceFile 源文件
    * @param zos zip输出流
    * @param name 压缩后的名称
    * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
    * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
    * @throws Exception
    */
    private static void compress(File sourceFile, ZipOutputStream zos, String name,
    boolean KeepDirStructure) throws Exception{
    byte[] buf = new byte[2 * 1024];
    if(sourceFile.isFile()){
    // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
    zos.putNextEntry(new ZipEntry(name));
    // copy文件到zip输出流中
    int len;
    FileInputStream in = new FileInputStream(sourceFile);
    while ((len = in.read(buf)) != -1){
    zos.write(buf, 0, len);
    }
    // Complete the entry
    zos.closeEntry();
    in.close();
    } else {
    File[] listFiles = sourceFile.listFiles();
    if(listFiles == null || listFiles.length == 0){
    // 需要保留原来的文件结构时,需要对空文件夹进行处理
    if(KeepDirStructure){
    // 空文件夹的处理
    zos.putNextEntry(new ZipEntry(name + "/"));
    // 没有文件,不需要文件的copy
    zos.closeEntry();
    }
    }else {
    for (File file : listFiles) {
    // 判断是否需要保留原来的文件结构
    if (KeepDirStructure) {
    // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
    // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
    compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
    } else {
    compress(file, zos, file.getName(),KeepDirStructure);
    }
    }
    }
    }
    }

    /**
    * 压缩成ZIP 方法1
    * @param srcDir 压缩文件夹路径
    * @param out 压缩文件输出流
    * @param KeepDirStructure 是否保留原来的目录结构,true:保留目录结构;
    * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
    * @throws RuntimeException 压缩失败会抛出运行时异常
    */
    public static void toZip1(String srcDir, OutputStream out, boolean KeepDirStructure)
    throws RuntimeException{
    long start = System.currentTimeMillis();
    ZipOutputStream zos = null ;
    try {
    zos = new ZipOutputStream(out);
    File sourceFile = new File(srcDir);
    compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
    long end = System.currentTimeMillis();
    System.out.println("压缩完成,耗时:" + (end - start) +" ms");
    } catch (Exception e) {
    throw new RuntimeException("zip error from ZipUtils",e);
    }finally{
    if(zos != null){
    try {
    zos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

    /**
    * 压缩成ZIP 方法2
    * @param srcFiles 需要压缩的文件列表
    * @param out 压缩文件输出流
    * @throws RuntimeException 压缩失败会抛出运行时异常
    */
    public static void toZip2(List<File> srcFiles , OutputStream out)throws RuntimeException {
    long start = System.currentTimeMillis();
    ZipOutputStream zos = null ;
    try {
    zos = new ZipOutputStream(out);
    for (File srcFile : srcFiles) {
    byte[] buf = new byte[2 * 1024];
    zos.putNextEntry(new ZipEntry(srcFile.getName()));
    int len;
    FileInputStream in = new FileInputStream(srcFile);
    while ((len = in.read(buf)) != -1){
    zos.write(buf, 0, len);
    }
    zos.closeEntry();
    in.close();
    }
    long end = System.currentTimeMillis();
    System.out.println("压缩完成,耗时:" + (end - start) +" ms");
    } catch (Exception e) {
    throw new RuntimeException("zip error from ZipUtils",e);
    }finally{
    if(zos != null){
    try {
    zos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

    /**
    * 得到图片的二进制数据,以二进制封装得到数据,具有通用性
    * @param inStream
    * @return
    * @throws Exception
    */
    public static byte[] readInputStream(InputStream inStream) throws Exception{
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    //创建一个Buffer字符串
    byte[] buffer = new byte[1024];
    //每次读取的字符串长度,如果为-1,代表全部读取完毕
    int len = 0;
    //使用一个输入流从buffer里把数据读取出来
    while( (len=inStream.read(buffer)) != -1 ){
    //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
    outStream.write(buffer, 0, len);
    }
    //关闭输入流
    inStream.close();
    //把outStream里的数据写入内存
    return outStream.toByteArray();
    }

    /**
    * 删除文件夹
    * @param folderPath 文件夹完整绝对路径
    * @return
    */
    public static void delFolder(String folderPath) {
    try {
    delAllFile(folderPath); //删除完里面所有内容
    String filePath = folderPath;
    filePath = filePath.toString();
    java.io.File myFilePath = new java.io.File(filePath);
    myFilePath.delete(); //删除空文件夹
    }
    catch (Exception e) {
    }
    }


    /**
    * 删除指定文件夹下所有文件
    * @param path 文件夹完整绝对路径
    * @return
    * @return
    */
    public static boolean delAllFile(String path) {
    boolean bea = false;
    File file = new File(path);
    if (!file.exists()) {
    return bea;
    }
    if (!file.isDirectory()) {
    return bea;
    }
    String[] tempList = file.list();
    File temp = null;
    for (int i = 0; i < tempList.length; i++) {
    if (path.endsWith(File.separator)) {
    temp = new File(path + tempList[i]);
    }else{
    temp = new File(path + File.separator + tempList[i]);
    }
    if (temp.isFile()) {
    temp.delete();
    }
    if (temp.isDirectory()) {
    delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
    delFolder(path+"/"+ tempList[i]);//再删除空文件夹
    bea = true;
    }
    }
    return bea;
    }
    }
  • 相关阅读:
    031-进阶(日志)
    Django 路由系统
    C++ 面向对象(接口-抽象类)
    C++ 面向对象(多态)
    C++ 面向对象(数据抽象)
    三十、首页列表显示全部问答,完成问答详情页布局
    二十九、制作首页的显示列表
    二十八、发布功能完成
    二十七、登录之后更新导航
    二十六、完成登录功能,用session记住用户名
  • 原文地址:https://www.cnblogs.com/wjxbk/p/16013177.html
Copyright © 2020-2023  润新知