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)

相关推荐
小飞侠在吗2 小时前
vue props
前端·javascript·vue.js
用户841794814564 小时前
vxe-gantt 甘特图实现产品进度列表,自定义任务条样式和提示信息
vue.js
RainbowSea5 小时前
从 Spring Boot 2.x 到 3.5.x + JDK21:一次完整的生产环境迁移实战
java·spring boot·后端
奶油松果5 小时前
Springboot自动装配 - redis和redission
java·spring boot·redis
千寻技术帮5 小时前
10413_基于Springboot的智慧养老院管理系统
spring boot·mysql·源码·安装·文档·ppt·养老院
VX:Fegn08955 小时前
计算机毕业设计|基于Java人力资源管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·课程设计
荔枝hu6 小时前
springboot和shiro组合引入SseEmitter的一些坑
java·spring boot·后端·sseeitter
老华带你飞6 小时前
健身房|基于springboot + vue健身房管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
JIngJaneIL6 小时前
基于Java酒店预约系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
一位搞嵌入式的 genius8 小时前
Vue实例挂载:从原理到项目实践的全维度解析
前端·javascript·vue.js·前端框架