post曼上传压缩包,后端解析并上传fastDFS

 @Override
    public ComponentCheckDTO importZip(MultipartFile multipartFile) throws IOException {
        ComponentCheckDTO componentCheckDTO = new ComponentCheckDTO();
        //获取ZIP输入流(指定字符集)
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(multipartFile.getInputStream()), StandardCharsets.UTF_8);

        //定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
        ZipEntry ze = null;

        //循环遍历
        while ((ze = zipInputStream.getNextEntry()) != null) {
            System.out.println("文件名:" + ze.getName() + " 文件大小:" + ze.getSize() + " bytes");

            String path = System.getProperty("java.io.tmpdir");
            String fileName = ze.getName();
            String fileFullName = path + "\\" + fileName;
            if (ze.isDirectory()) {
                createDirIfNotExist(fileFullName);
            } else {
                System.out.println("文件内容:");
                File file = new File(fileFullName);
                createFileIfNotExist(file);
                FileOutputStream fos = new FileOutputStream(fileFullName);
                int tmp;
                byte[] b = new byte[1024];
                StringBuffer stringBuffer = new StringBuffer();
                while ((tmp = zipInputStream.read(b)) != -1) {
                    //写入到目标文件中
                    fos.write(b, 0, tmp);
                    String data = new String(b, 0, tmp);
                    stringBuffer.append(data);
                    System.out.println(data);
                }
                fos.close();
                // 上传fastDFS
                FileInputStream fileInputStream = new FileInputStream(file);
                MultipartFile uploadFile = new MockMultipartFile("file", file.getName(),
                        ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
                JSONObject uploadFileObject = fileFeignService.uploadFile(uploadFile);
                if ("0".equals(uploadFileObject.get("code"))) {
                    JSONObject data = JSONUtil.parseObj(uploadFileObject.get("data"));
                    String fastDFSId = data.getStr("id");
                    String filePath = data.getStr("filePath");
                    String[] split = filePath.split("/");
                    String fastDFSName = split[split.length - 1];

                }


                if (StringUtils.isNotBlank(stringBuffer) && stringBuffer.toString().startsWith("{")) {
                    // todo 存入映射管理表
                    JSONObject jsonObject = JSONUtil.parseObj(stringBuffer);
                    System.out.println("jsonObject: " + jsonObject);
                }

                // todo 删除文件
                boolean delete = file.delete();
                System.out.println(delete ? "删除成功!" : "删除失败...");
            }

            zipInputStream.closeEntry();

        }

        //关闭流
        zipInputStream.closeEntry();
        return componentCheckDTO;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值