Java | 使用Zip方式批量导入图片


导入的ZIP样式

在这里插入图片描述


导入数据接收类

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * 产品 - 图片导入
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class PictureImportVo implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("导入的行编号")
    private Integer rowNum;

    @ApiModelProperty("产品ID")
    private String productId;

    @ApiModelProperty(value = "产品编码")
    private String productNumber;

    /**
     * 是否主图(0:否,1:是)
     */
    @ApiModelProperty("是否主图")
    private Integer isMain;

    @ApiModelProperty(value = "图片")
    private byte[] imageByte;

    @ApiModelProperty(value = "图片名称")
    private String imageName;
}

实现方法

import org.apache.commons.io.FilenameUtils;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@ApiOperation("产品图片导入")
@PostMapping("/imageImportZip")
public ResponseData<Boolean> imageImportZip(@RequestPart("file") MultipartFile file) {
    if (!Objects.equals(FilenameUtils.getExtension(file.getOriginalFilename()), "zip")) {
        throw new RuntimeException("导入异常,文件非zip!");
    }

	// 存储导入的数据
    List<WareProductInfoPictureImportVo> voList = new ArrayList<>();

	// zip文件里含有中文名称的文件,windows 环境下,默认字符集为GBK,ZipFile 默认使用 UTF-8 字符集,当文件名存在中文时,处理时就会报错
	// 转GBK是为了防止java.lang.IllegalArgumentException: MALFORMED
    try (ZipInputStream zis = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"))) {
        ZipEntry nextEntry = zis.getNextEntry();

        // 遍历zip文件中的每个条目
        while (nextEntry != null) {
            String entryName = nextEntry.getName();
            if(entryName.contains("/")){
                entryName = entryName.split("/")[1];
            }

            if(containsChinese(entryName)){
                throw new RuntimeException("图片名称包含中文:" + entryName);
            }

            // 不是目录,且是图片的文件
            if (!nextEntry.isDirectory() && (entryName.endsWith(".jpg") || entryName.endsWith(".png") || entryName.endsWith(".gif")
                    || entryName.endsWith(".jpeg"))) {

                // 产品编码_是否首选
                String[] split = entryName.split("\\.")[0].split("_");
                if(split.length == 2){
                    PictureImportVo vo = new PictureImportVo ();
                    vo.setImageName(entryName);
                    vo.setProductNumber(split[0]);
                    vo.setIsMain(Integer.valueOf(split[1]));
                    vo.setImageByte(extractFile(zis));
                    voList.add(vo);
                }
            }
            zis.closeEntry();
            nextEntry = zis.getNextEntry();
        }
        zis.close();
    } catch (IOException e) {
        throw new RuntimeException("解析产品图片异常:" + e.getMessage());
    }

	// 其他逻辑处理....
	
    return ResponseData.ok(Boolean.TRUE);
}

public boolean containsChinese(String str) {
    String regEx = "[\\u4e00-\\u9fa5]";
    Pattern pattern = Pattern.compile(regEx);
    Matcher matcher = pattern.matcher(str);
    return matcher.find();
}

private byte[] extractFile(ZipInputStream zipIn) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] bytesIn = new byte[4096];
    int read = 0;

    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    return bos.toByteArray();
}

其他方式批量导入图片:使用Excel批量导入图片


欢迎关注公众号:慌途L
后面会慢慢将文章迁移至公众号,也是方便在没有电脑的情况下可以进行翻阅,更新的话会两边同时更新,大家不用担心!
在这里插入图片描述


  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值