后台压缩,解压。批量上传

不废话上代码之【后台压缩,解压。批量上传】

需求:需要上传api 压缩包(里面是图片) 

@Override
public int insertXcxCard(XcxCard xcxCard) {

    //判断是否为空
    String uploadUrl = xcxCard.getUploadUrl();
    if (StringUtils.isEmpty(uploadUrl)) {
        throw new ServiceException("导入文件不能为空");
    }
    //获取解压后文件位置  id新创建的目标为了不重复
    Integer id = zipDecompression(uploadUrl);
    //获取最后一个 / 的位置
    int i = uploadUrl.lastIndexOf("/");
    //获取文件的目录
    String substring = uploadUrl.substring(0, i);
    // 找到对应的文件位置
    File file = new File(resourceLocation + substring.replace("/profile", "") + "/" + id);
    //获取文件
    String[] strings = FileUtil.listFiles(file);
    if (strings.length == 0) {
        throw new ServiceException("上传文件不能为空");
    }
    //添加记录
    for (String zipUrl : strings) {
    // 有些压缩会创建 __MACOSX 目录;过滤目录,只去图片
        if(zipUrl.indexOf("__MACOSX") > -1 || !(zipUrl.toLowerCase().endsWith(".png") || zipUrl.toLowerCase().endsWith(".jpg") || zipUrl.toLowerCase().endsWith(".bmp") || zipUrl.toLowerCase().endsWith(".jpeg"))){
            continue;
        }
        // 判断 配置的价格是否存在
        Integer configId = xcxCard.getConfigId();
        XcxCardConfig xcxCardConfig = xcxCardConfigMapper.selectById(configId);
        if (null == xcxCardConfig) {
            throw new ServiceException("价格配置有误");
        }
        //新增记录
        xcxCard.setCardPrice(xcxCardConfig.getCardPrice());
        xcxCard.setFaceValue(xcxCardConfig.getFaceValue());
        xcxCard.setCardName(xcxCardConfig.getCardName());
        xcxCard.setCardPic(xcxCardConfig.getCardPic());
        xcxCard.setUseContent(xcxCardConfig.getUseContent());
        // 压缩后文件的位置
        xcxCard.setCheckUrl(substring + "/" + id + "/" + zipUrl);
        xcxCard.setCheckName(zipUrl);
        xcxCardMapper.insert(xcxCard);
    }
    return 1;
}

方法

/**
 * 压缩包处理
 *
 * @param uploadUrl
 * @return
 */
private Integer zipDecompression(String uploadUrl) {
    //新增记录 获取唯一目录
    XcxCardUploadRecord xcxCardUploadRecord = new XcxCardUploadRecord();
    xcxCardUploadRecord.setUploadUrl(uploadUrl);
    xcxCardUploadRecordMapper.insert(xcxCardUploadRecord);

    //取直接访问路径
    uploadUrl = uploadUrl.replace("/profile", "");
    int i = uploadUrl.lastIndexOf("/");
    String substring = uploadUrl.substring(0, i);
    Integer id = xcxCardUploadRecord.getId();
    // 解压到对应位置
    ZipUtil.unzip(resourceLocation + uploadUrl, resourceLocation + substring + "/" + id,Charset.forName("UTF-8"));//
    //解压到的文件夹
    xcxCardUploadRecord.setDecompressionUrl("/profile" + substring + "/" + id);
    xcxCardUploadRecordMapper.updateById(xcxCardUploadRecord);
    return id;
}

上传的文件存在中文 encodeURIComponent

<el-table-column label="核销码" align="center" prop="checkUrl" width="100">
  <template slot-scope="scope">
    <image-preview :src="encodeURIComponent(scope.row.checkUrl)" :width="50" :height="50"/>
  </template>
</el-table-column>

//后续遇到压缩文件名称中文和压缩包中的文件为中文

解决方案 第一个抛异常 执行第二个

try {
    // 可能会抛出异常的代码
    // 解压到对应位置
    ZipUtil.unzip(resourceLocation + uploadUrl, resourceLocation + substring + "/" + id, Charset.forName("UTF-8"));
} catch (Exception e) {
    ZipUtil.unzip(resourceLocation + uploadUrl, resourceLocation + substring + "/" + id, Charset.forName("GBK"));
} finally {
    System.out.println("++++++++++解压+++++++++++");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值