gaoluyang
21 小时以前 abd339106ed3a6465293edccd33e5f87bd43bdde
cnas-manage/src/main/java/com/ruoyi/manage/service/impl/ManageDocumentListServiceImpl.java
@@ -11,6 +11,7 @@
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;
@@ -25,6 +26,8 @@
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;
@@ -58,29 +61,29 @@
    @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);