| | |
| | | |
| | | |
| | | import com.ruoyi.common.utils.QueryWrappers; |
| | | import com.ruoyi.framework.exception.ErrorException; |
| | | import com.ruoyi.manage.mapper.ManageDocumentListMapper; |
| | | import com.ruoyi.manage.pojo.ManageDocumentList; |
| | | import com.ruoyi.manage.service.ManageDocumentListService; |
| | |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.StandardCopyOption; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | |
| | | @Override |
| | | public int uploadFile(Integer id, MultipartFile file) { |
| | | String urlString; |
| | | String pathName; |
| | | String path; |
| | | ManageDocumentList manageDocumentList = manageDocumentListMapper.selectById(id); |
| | | if (ObjectUtils.isNotEmpty(manageDocumentList.getUrl())){ |
| | | // 删除旧文件 |
| | | File oldFile = new File(wordUrl + "/" + manageDocumentList.getUrl()); |
| | | oldFile.delete(); |
| | | if (manageDocumentList == null) { |
| | | throw new ErrorException("文件清单不存在"); |
| | | } |
| | | //上传新文件 |
| | | path = wordUrl; |
| | | if (file == null || file.isEmpty()) { |
| | | throw new ErrorException("请选择要上传的文件"); |
| | | } |
| | | File realpath = new File(wordUrl).getAbsoluteFile(); |
| | | try { |
| | | File realpath = new File(path); |
| | | if (!realpath.exists()) { |
| | | realpath.mkdirs(); |
| | | if (!realpath.exists() && !realpath.mkdirs()) { |
| | | throw new IOException("目录创建失败: " + realpath); |
| | | } |
| | | pathName = UUID.randomUUID() + "_" + file.getOriginalFilename(); |
| | | urlString = realpath + "/" + pathName; |
| | | file.transferTo(new File(urlString)); |
| | | // 用 Files.copy 而不是 transferTo:transferTo 遇到相对路径会按 Tomcat 临时目录解析 |
| | | Files.copy(file.getInputStream(), new File(realpath, pathName).toPath(), StandardCopyOption.REPLACE_EXISTING); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | System.err.println("附件上传错误"); |
| | | return 0; |
| | | throw new ErrorException("附件上传错误: " + e.getMessage()); |
| | | } |
| | | if (ObjectUtils.isNotEmpty(manageDocumentList.getUrl())) { |
| | | // 删除旧文件 |
| | | new File(realpath, manageDocumentList.getUrl()).delete(); |
| | | } |
| | | manageDocumentList.setUrl(pathName); |
| | | return manageDocumentListMapper.updateById(manageDocumentList); |