| | |
| | | package com.javaweb.geo.service.impl; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.nio.charset.Charset; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.ZipUtil; |
| | | import com.javaweb.common.config.Global; |
| | | import com.javaweb.common.core.domain.AjaxResult; |
| | | import com.javaweb.common.exception.BusinessException; |
| | | import com.javaweb.common.utils.DateUtils; |
| | | import com.javaweb.common.utils.IdGenerate; |
| | | import com.javaweb.common.utils.StringUtils; |
| | | import com.javaweb.geo.domain.Hole; |
| | | import com.javaweb.geo.domain.HoleMedia; |
| | | import com.javaweb.geo.mapper.HoleMapper; |
| | | import com.javaweb.geo.mapper.HoleMediaMapper; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import com.javaweb.geo.mapper.ProjectDataMapper; |
| | | import com.javaweb.geo.domain.ProjectData; |
| | | import com.javaweb.geo.service.IProjectDataService; |
| | | import com.javaweb.common.core.text.Convert; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | |
| | | public class ProjectDataServiceImpl implements IProjectDataService { |
| | | |
| | | |
| | | private static String manUploadPath = "imgupload"; |
| | | private static String zipfile = "zipfile"; |
| | | private static String zipfile2 = "zipfile2"; |
| | | |
| | | private static String imgUploadPath = Global.getProfile() + "\\" + manUploadPath + "\\"; |
| | | //zip 原始文件 |
| | | private static String ZIPUploadPath = Global.getProfile() + "\\" + zipfile + "\\"; |
| | | |
| | | //解压文件 |
| | | private static String ZIPUploadPath2 = Global.getProfile() + "\\" + zipfile2 + "\\"; |
| | | |
| | | //单文件存储路径 |
| | | private static String ZIPUploadPath2SingleFile = Global.getProfile() + "\\" + zipfile2 + "\\img\\"; |
| | | |
| | | @Autowired |
| | | private ProjectDataMapper projectDataMapper; |
| | | @Autowired |
| | | private HoleMapper holeMapper; |
| | | |
| | | @Autowired |
| | | private HoleMediaMapper mediaMapper; |
| | | |
| | | |
| | | /** |
| | | * 查询项目资料 |
| | |
| | | return projectDataMapper.deleteProjectDataById(id); |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | public AjaxResult uploadZIP(MultipartFile file, HttpServletRequest request) { |
| | | String uploadPath = Global.getProfile() + "\\"; |
| | | public AjaxResult uploadZIP(MultipartFile file, HttpServletRequest request) { |
| | | |
| | | String originalFilename = file.getOriginalFilename(); |
| | | |
| | | String fieldname = request.getParameter("fieldname"); |
| | | if (StringUtils.isEmpty(fieldname)) { |
| | | fieldname = "filename"; |
| | | //传入的钻孔的id |
| | | String projectDataId = request.getParameter("projectDataId"); |
| | | |
| | | //上传单文件 |
| | | if(ObjectUtil.isNotEmpty(originalFilename) && !originalFilename.endsWith(".zip")){ |
| | | uploadSingleFile(file,projectDataId); |
| | | return AjaxResult.success("上传成功"); |
| | | } |
| | | //上传ZIP文件 |
| | | |
| | | //获取后缀.zip 保存的文件名 |
| | | String substring = originalFilename.substring(originalFilename.lastIndexOf(".")); |
| | | String UID = IdGenerate.nextId(); |
| | | String dFileName = UID + substring; |
| | | |
| | | File file2 = new File(uploadPath + dFileName); |
| | | File file2 = new File(ZIPUploadPath + dFileName); |
| | | try { |
| | | file.transferTo(file2); |
| | | } catch (Exception e) { |
| | |
| | | } catch (Exception e) { |
| | | return AjaxResult.warn("附件解压缩失败!"); |
| | | } |
| | | String moveDir = Global.getProfile() + "\\" + UID + "\\" + originalFilename.replace(substring, ""); |
| | | List<String> movedFiles = moveFile(moveDir); |
| | | updateFilePath(fieldname, movedFiles); |
| | | |
| | | FileUtil.del(new File(uploadPath + UID)); |
| | | FileUtil.del(file2); |
| | | //移动到zipfile2 存储解压完毕的文件 |
| | | String moveDir = ZIPUploadPath + UID; |
| | | moveFile(moveDir); |
| | | |
| | | //更新数据库 |
| | | updateFilePath(projectDataId, ZIPUploadPath2 + UID ); |
| | | |
| | | FileUtil.del(new File(ZIPUploadPath + UID)); |
| | | |
| | | return AjaxResult.success("上传成功"); |
| | | } |
| | | |
| | | //移动 文件 |
| | | private List<String> moveFile(String moveDir) { |
| | | List<String> list = new ArrayList<>(); |
| | | File file = new File(moveDir); |
| | | String detpath = imgUploadPath; |
| | | if (file.isDirectory()) { |
| | | File[] files = file.listFiles(); |
| | | int len = files.length; |
| | | for (int i = 0; i < len; i++) { |
| | | String filename = files[i].getName(); |
| | | list.add(filename); |
| | | FileUtil.copy(files[i], new File(detpath + filename), true); |
| | | } |
| | | //上传单文件 |
| | | private void uploadSingleFile(MultipartFile file,String projectDataId) { |
| | | |
| | | ProjectData projectData = projectDataMapper.selectProjectDataById(projectDataId); |
| | | String projectId= projectData.getProjectId(); |
| | | |
| | | String originalFilename= file.getOriginalFilename(); |
| | | //获取后缀 保存的文件名 |
| | | String substring = originalFilename.substring(originalFilename.lastIndexOf(".")); |
| | | String UID = IdGenerate.nextId(); |
| | | String dFileName = UID + substring; |
| | | |
| | | String filepath = ZIPUploadPath2SingleFile + dFileName; |
| | | |
| | | File destFile = new File(filepath); |
| | | |
| | | try{ |
| | | file.transferTo(destFile); |
| | | }catch (Exception e){ |
| | | throw new BusinessException("保存失败!",e); |
| | | } |
| | | return list; |
| | | |
| | | String url = filepath.replace(Global.getProfile(),"").replace("\\","/"); |
| | | |
| | | HoleMedia media = new HoleMedia(); |
| | | media.setIds(IdGenerate.nextId()); |
| | | media.setRecordId(projectDataId); |
| | | media.setProjectId(projectId); |
| | | media.setName(originalFilename); |
| | | media.setInternetPath(url); |
| | | media.setCreateTime(DateUtils.getNowDate()); |
| | | mediaMapper.insertHoleMedia(media); |
| | | |
| | | } |
| | | |
| | | private void updateFilePath(String filedname, List<String> filesPath) { |
| | | //移动 文件 |
| | | private void moveFile(String moveDir) { |
| | | File file = new File(moveDir); |
| | | File dest = new File(ZIPUploadPath2); |
| | | FileUtil.move(file,dest,true); |
| | | } |
| | | |
| | | for (String fileName : filesPath) { |
| | | String substring = fileName.substring(fileName.lastIndexOf(".")); |
| | | String docFileName = fileName.replace(substring, "").trim(); |
| | | ProjectData param = new ProjectData(); |
| | | |
| | | if (filedname.equals("filename")) { |
| | | param.setName(docFileName); |
| | | } |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateFilePath(String projectDataId, String dirpath) { |
| | | |
| | | List<ProjectData> list = projectDataMapper.selectProjectDataList(param); |
| | | if (!ObjectUtils.isEmpty(list)) { |
| | | for (ProjectData item : list) { |
| | | ProjectData entity = new ProjectData(); |
| | | entity.setId(item.getId()); |
| | | entity.setDataUrl(manUploadPath + "/" + fileName); |
| | | projectDataMapper.updateProjectData(entity); |
| | | } |
| | | ProjectData projectData = projectDataMapper.selectProjectDataById(projectDataId); |
| | | String projectId= projectData.getProjectId(); |
| | | File fileDir = new File(dirpath); |
| | | List<File> files = FileUtil.loopFiles(fileDir); |
| | | |
| | | if(ObjectUtil.isNotEmpty(files)){ |
| | | for (File file : files){ |
| | | String filename = file.getName(); |
| | | String filepath = file.getPath(); |
| | | String url = filepath.replace(Global.getProfile(),"").replace("\\","/"); |
| | | HoleMedia media = new HoleMedia(); |
| | | media.setIds(IdGenerate.nextId()); |
| | | media.setRecordId(projectDataId); |
| | | media.setProjectId(projectId); |
| | | media.setName(filename); |
| | | media.setInternetPath(url); |
| | | media.setCreateTime(DateUtils.getNowDate()); |
| | | mediaMapper.insertHoleMedia(media); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | } |