Springboot + Vue 上传Word、PDF文档并保留内部格式

因为业务需求,上传Word文件需要编辑,但如何使用Blob方式,在数据库里存文件,就会造成格式消失。所以修改思路:上传文件到服务器本地,保证数据存储的完整性。

前端

html 复制代码
              <el-upload class="upload-demo" :action="item.fileUploadUrl" :show-file-list="false" :limit="1" :multiple="false" :auto-upload="true" :accept="item.accept" :before-upload="beforeUpload"
                :headers="item.uploadHeaders" :data="item.uploadData">上传
              </el-upload></el-button>
              
javascript 复制代码
     fileActionList: [
        {
          width: 60,
          name: "合同文件",
          fileUploadUrl: "*************/importContract",
          uploadData: {},
          uploadHeaders: {},
          accept: ".doc,.docx,.pdf",
        },
      ],



    beforeUpload(file, fileList) {
      let promise = new Promise((resolve) => {
        this.$nextTick(function () {
          resolve(true);
        });
      });
      this.$message.success("上传成功");

      return promise;
    },
        // 文件上传操作
    handleImport(row) {
      this.fileActionList[0].uploadData = {
        id: row.id,
      };
      this.fileActionList[0].uploadHeaders = {
        Authorization: Cookie.get("*******"),
      };
    },

就是常规的传文件

后端

java 复制代码
    @ApiOperation("导入合同文件")
    @PostMapping("importContract")
    public Result importData(@RequestParam(value="file") MultipartFile file, @RequestParam(value="id") Long id) throws Exception {
        return projectService.importData(file, id);
    }
        @Override
    public Result importData(MultipartFile file, Long id) throws Exception {
        //获取文件名
        InputStream path = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            path = file.getInputStream();
            fis = (FileInputStream) path;
            File dir = new File("file/project");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            String filePath = "file/project/" + id + "_" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
            fos = new FileOutputStream(filePath);
            int readlen = 0;
            //字节数组,一次读取8个字节
            byte[] buf = new byte[8];
            while ((readlen = fis.read(buf)) != -1) {
                fos.write(buf, 0, readlen);
            }
            Project project = projectRepo.getById(id);
            project.setContactFile(filePath);
            projectRepo.save(project);
        } catch (IOException e) {
            log.error("" + e);
        } finally {
            path.close();
            fis.close();
            fos.close();
        }
        return Result.success("上传成功");
    }

这此采用的思路就是 把文件读取后,写入相对路径,考虑到文件业务误传,服务器源文件不删除,通过时间戳保证唯一性。存到服务器本地。

可以看到,最后格式都保留了下来

![在这里插入图片描述](https://img-blog.csdnimg.cn/6c7c61ef8085404ea23c4be0ca6c6f19.png![在这里插入图片描述](https://file.jishuzhan.net/article/1681602352328478722/cd8d0d203ebd497582691154d2df79e4.png)

相关推荐
架构师沉默26 分钟前
我用一个 Postgres 实现一整套后端架构!
java·spring boot·程序人生·架构·tdd
xiucai_cs30 分钟前
布隆过滤器原理与Spring Boot实战
java·spring boot·后端·布隆过滤器
巴拉巴巴巴拉31 分钟前
Spring Boot 中 YAML 配置文件详解
spring boot
向阳花自开32 分钟前
Spring Boot 常用注解速查表
java·spring boot·后端
20262 小时前
13.2 ssr基本原理,构建步骤
前端·vue.js
一只爱撸猫的程序猿2 小时前
创建一个使用Spring AI结合MCP(Model Context Protocol)和Dify构建智能客服系统的简单案例
spring boot·aigc·ai编程
前端开发爱好者2 小时前
首个「完整级」WebSocket 调试神器来了!
前端·javascript·vue.js
黑幕困兽3 小时前
vue 项目给输入框增加trim()方法
vue.js
꧁༺摩༒西༻꧂3 小时前
Spring Boot Actuator 监控功能的简介及禁用
java·数据库·spring boot
龙国浪子3 小时前
从零构建桌面写作软件的书籍管理系统:Electron + Vue 3 实战指南
vue.js·electron