whycxzp
2022-09-05 b12820b371835e7ff3984636cebbe3f1caeaddaf
src/main/java/com/whyc/service/MaterialService.java
@@ -12,6 +12,7 @@
import com.whyc.pojo.ProductBom;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import com.whyc.util.DateUtil;
import com.whyc.util.FileUtil;
import com.whyc.util.Zip4jUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -26,10 +27,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
@@ -358,14 +356,66 @@
    }
    /**
     *
     * @param multipartFile 被追加的附件
     * @param multipartFile 被追加的附件,zip内只能的附件都必须是文件
     * @param material
     * @return 返回被追加文件的文件夹路径
     */
    public Response addAttachment(MultipartFile multipartFile, Material material) {
        String dirPath = "doc_file" + File.separator + "material" + File.separator + material.getId() + material.getSubCode() + material.getSubModel();
        //TODO
        return new Response().setII(1,"上传完成");
    @Transactional
    public Response addAttachment(MultipartFile multipartFile, Material material) throws IOException {
        List<String> currentFileList = new LinkedList<>();
        List<String> uploadingFileList = new LinkedList<>();
        //String dirPath = "doc_file" + File.separator + "material" + File.separator + material.getId() + material.getSubCode() + material.getSubModel();
        String dirPath = CommonUtil.getRootFile() + "material" + File.separator + material.getId() + material.getSubCode() + material.getSubModel();
        File dirFile = new File(dirPath);
        String originalFilename = multipartFile.getOriginalFilename();
        Date now = new Date();
        long timestamp = now.getTime();
        String formattedTimestamp = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(now);
        //压缩包文件夹校验
        File zipFile = new File(dirPath + File.separator + originalFilename);
        multipartFile.transferTo(zipFile);
        //解压文件夹,删除原zip文件
        String unPackageDir = dirPath + File.separator + timestamp;
        Zip4jUtil.unPackZip(zipFile, null, unPackageDir);
        File unPackageFile = new File(unPackageDir);
        File[] unPackageFileList = unPackageFile.listFiles();
        for (int i = 0; i < unPackageFileList.length; i++) {
            File tempFile = unPackageFileList[i];
            if(tempFile.isDirectory()){
                FileUtil.deleteFile(unPackageFile);
                return new Response().set(1,false,"拒绝上传:上传的压缩包存在文件夹");
            }
        }
        zipFile.delete();
        if(!dirFile.exists()){
            dirFile.mkdirs();
            //路径不存在,说明是首次上传,设置物料对应的fileUrl
            Material temp = new Material();
            temp.setId(material.getId());
            temp.setFileUrl(dirPath);
            mapper.updateById(temp);
        }else{
            //查询路径下现有的所有文件
            String[] fileArr = dirFile.list();
            currentFileList = Arrays.asList(fileArr);
        }
        //查询新上传的文件路径
        String[] unPackageFileArr = unPackageFile.list();
        uploadingFileList = Arrays.asList(unPackageFileArr);
        for (String uploadingFile : uploadingFileList){
            if(currentFileList.contains(uploadingFile)){
                new File(unPackageDir + File.separator + uploadingFile)
                        .renameTo(new File(unPackageDir + File.separator + formattedTimestamp + uploadingFile));
            }
        }
        //上传的文件夹转移到正式路径
        org.aspectj.util.FileUtil.copyDir(unPackageFile,dirFile);
        return new Response().set(1,true,"上传完成");
    }
}