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批量导入图片
- Java | 使用Excel批量导入图片:https://editor.csdn.net/md/?articleId=136195214
欢迎关注公众号:慌途L
后面会慢慢将文章迁移至公众号,也是方便在没有电脑的情况下可以进行翻阅,更新的话会两边同时更新,大家不用担心!