springboot将文件插入到指定路径文件夹,判断文件是否存在以及根据名称删除

解决方案:

直接使用File和数据流实现

1.插入这个是我自己的业务我是将文件转成pdf之后存入指定路径了这个路径是直接定义在了yml里然后在需要的地方注入调用,然后通过file创建空白pdf放到数据流通过:aspose的方法保存

复制代码
file:
  upload:
    dir: D:/data/uploads       # 通用文件上传目录
    pdfDir: D:/data/uploadPDF  # PDF文件专用目录
    dowFile: C:\Users\Lenovo\Desktop #下载路径
复制代码
@Value("${file.upload.pdfDir}")
private String uploadDirPDF;
String fullSourcePath = uploadDir + File.separator + fileName;
String fullPdfPath = uploadDirPDF + File.separator + pdfFileName;
复制代码
public static void docToPdf(String sourcerFile,String targetFile) {
    if (!getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生
        return;
    }
    try {
        long old = System.currentTimeMillis();
        File file = new File(targetFile);  //新建一个空白pdf文档
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        Document doc = new Document(sourcerFile);                    //sourcerFile是将要被转化的word文档
        doc.save(out, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        out.close();
        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
    } catch (Exception e) {
        e.printStackTrace();
    }
}


2.判断文件是否存在
获取路径传入file即可然后调用exists返回来的内容就可以实现
File pdfDir = new File(uploadDirPDF);
if (!pdfDir.exists() && !pdfDir.mkdirs()) {
    fileData.setRemark("无法创建PDF目录: " + uploadDirPDF);
    return fileData;
}

3.根据名称删除文件
先将数据库的内容删除,数据库删除没有错误成功后删除文档,将名称和路径传入到方法里调用file.delete即可
@RequestMapping("/delMenusTree")
public R delMenusTree(@RequestBody MenuDeleteDTO menuDeleteDTO){
    String msg= biddingtoolsoftwareService.delMenusTree(menuDeleteDTO.getId());
    if (msg.equals("删除成功")){
        String mlname = menuDeleteDTO.getMlname();
        deleteFileIfExists(uploadDir, mlname);
        deleteFileIfExists(uploadDirPDF, mlname);
    }
    return R.ok(msg);
}
private void deleteFileIfExists(String directory, String fileName) {
    if (fileName == null || fileName.isEmpty()) {
        return; // 避免空文件名导致异常
    }

    File file = new File(directory, fileName);
    if (file.exists() && file.isFile()) {
        if (file.delete()) {
            log.info("成功删除文件: {}", file.getAbsolutePath());
        } else {
            log.error("删除文件失败: {}", file.getAbsolutePath());
            // 可以选择记录日志或返回更详细的错误信息
        }
    }
}
相关推荐
章豪Mrrey nical6 小时前
前后端分离工作详解Detailed Explanation of Frontend-Backend Separation Work
后端·前端框架·状态模式
超级大只老咪7 小时前
数组相邻元素比较的循环条件(Java竞赛考点)
java
小浣熊熊熊熊熊熊熊丶7 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
毕设源码-钟学长7 小时前
【开题答辩全过程】以 公交管理系统为例,包含答辩的问题和答案
java·eclipse
啃火龙果的兔子7 小时前
JDK 安装配置
java·开发语言
星哥说事7 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
派大鑫wink7 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
程序员爱钓鱼7 小时前
Node.js 编程实战:文件读写操作
前端·后端·node.js
xUxIAOrUIII8 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home8 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法