| | |
| | | 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; |
| | |
| | | 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 |
| | |
| | | flag=true; |
| | | for (int i=0;i<fileNames.length;i++){ |
| | | if(!fileNames[i].contains("-dwg.pdf")&&!fileNames[i].contains("-doc.pdf")) |
| | | list.add(material.getFileUrl()+fileNames[i]); |
| | | list.add(material.getFileUrl()+File.separator+fileNames[i]); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | return new Response().setII(1,flag,list,"文件列表返回"); |
| | | } |
| | | //根据物料型号('-','_'之前的部分)查询物料的历史版本 |
| | | public Response getMaterialVersion(String subModel) { |
| | | List list=mapper.getMaterialVersion(subModel); |
| | | return new Response().setII(1,list.size()>0,list,"物料历史版本数据返回"); |
| | | } |
| | | //根据物料id查询物料信息 |
| | | public Response getMaterialById(int materialId) { |
| | | Material material=mapper.getMaterialById(materialId); |
| | | String fileDirName = FileDirPath.getFileDirName(); |
| | | List list=new ArrayList(); |
| | | if(material.getFileUrl()!=null&&!material.getFileUrl().isEmpty()){ |
| | | File file = new File(fileDirName+File.separator+material.getFileUrl()); |
| | | if(file.exists()) { |
| | | String[] fileNames = file.list();//获取该文件夹下的所有文件以及目录的名字 |
| | | if(fileNames.length>0){ |
| | | for (int i=0;i<fileNames.length;i++){ |
| | | if(!fileNames[i].contains("-dwg.pdf")&&!fileNames[i].contains("-doc.pdf")) |
| | | list.add(material.getFileUrl()+File.separator+fileNames[i]); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | return new Response().setIII(1,material!=null,material,list,"物料历史版本数据返回"); |
| | | } |
| | | |
| | | /** |
| | | * @param multipartFile 被追加的附件,zip内只能的附件都必须是文件 |
| | | * @param material |
| | | * @return 返回被追加文件的文件夹路径 |
| | | */ |
| | | @Transactional |
| | | public Response addAttachment(MultipartFile multipartFile, Material material) throws IOException { |
| | | List<String> currentFileList = new LinkedList<>(); |
| | | List<String> uploadingFileList = new LinkedList<>(); |
| | | |
| | | String dirPathDB = "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); |
| | | boolean dirFileExist = true; |
| | | if(!dirFile.exists()){ |
| | | dirFile.mkdirs(); |
| | | dirFileExist = false; |
| | | } |
| | | multipartFile.transferTo(zipFile); |
| | | //解压文件夹,删除原zip文件 |
| | | String unPackageDir = dirPath + File.separator + timestamp; |
| | | Zip4jUtil.unPackZip(zipFile, null, unPackageDir); |
| | | File unPackageFile = new File(unPackageDir); |
| | | File[] unPackageFileList = unPackageFile.listFiles(); |
| | | zipFile.delete(); |
| | | for (int i = 0; i < unPackageFileList.length; i++) { |
| | | File tempFile = unPackageFileList[i]; |
| | | if(tempFile.isDirectory()){ |
| | | FileUtil.deleteFile(unPackageFile); |
| | | if(!dirFileExist){ |
| | | FileUtil.deleteFile(dirFile); |
| | | } |
| | | return new Response().set(1,false,"拒绝上传:上传的压缩包存在文件夹"); |
| | | } |
| | | } |
| | | |
| | | if(!dirFileExist){ |
| | | //路径不存在,说明是首次上传,设置物料对应的fileUrl |
| | | Material temp = new Material(); |
| | | temp.setId(material.getId()); |
| | | temp.setFileUrl(dirPathDB); |
| | | 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 + uploadingFile.substring(0,uploadingFile.lastIndexOf(".")) + "_" + formattedTimestamp + uploadingFile.substring(uploadingFile.lastIndexOf(".")))); |
| | | } |
| | | } |
| | | //上传的文件夹转移到正式路径 |
| | | org.aspectj.util.FileUtil.copyDir(unPackageFile,dirFile); |
| | | FileUtil.deleteFile(unPackageFile); |
| | | |
| | | return new Response().set(1,true,"上传完成"); |
| | | } |
| | | } |