SpringBoot+Vue实现简单的文件上传(txt篇)

SpringBoot+Vue实现简单的文件上传

1 环境 SpringBoot 3.2.1,Vue 2,ElementUI
2 页面在这里插入图片描述

3 效果:只能上传txt文件且大小限制为2M,选择文件后自动上传。
4 前端代码

<template>
  <div class="container">
    <el-upload
        class="upload-demo"
        drag
        action="/xml/fileUpload"
        multiple
        accept=".txt"
    :before-upload="beforeUpload">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
      <div class="el-upload__tip"><slot name="tip" > 只能上传 txt 文件,且不超过2M</slot></div>
    </el-upload>
  </div>
</template>

<script>
// import axios from "axios";

export default {
  name: 'App',
  data() {
    const data = [];
    return {
      
    }
  },
  watch: {},
  created() {
  },
  methods: {
    beforeUpload(file){
      console.log(file.type)
      const isText = file.type == "text/plain"
      const isLt2M = file.size /1024 /1024 < 2
      if(!isText){
        this.$message.error("只能上传txt文件!")
      }
      if(!isLt2M){
        this.$message.error("文件大小超过限制!")
      }
    }

  }
}
</script>

<style>
.container {
  display: flex;
}
</style>

5 后端代码

package org.wjg.onlinexml.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.wjg.onlinexml.po.Result;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
public class FileController {

    @RequestMapping("/fileUpload")
    private Result getXml(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.builder().code(500).msg("上传失败!").build();
        }

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 在这里处理读取到的每一行内容
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.builder().code(200).msg("上传成功").build();
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Excel文件到数据库有多种方法,以下是一种基于Spring Boot和Vue实现方式: 1. 前端页面实现Vue的前端页面中,添加一个文件组件,例如使用element-ui的el-upload组件: ```html <template> <el-upload class="upload-demo" action="/upload" // 文件的后端接口 :on-success="handleSuccess" :before-upload="beforeUpload"> <el-button size="small" type="primary">点击上</el-button> </el-upload> </template> ``` 2. 后端接口实现 在Spring Boot后端实现一个文件的接口,例如使用Spring Boot自带的MultipartFile实现: ```java @RestController public class FileUploadController { @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) { // 读取Excel文件,解析数据并插入到数据库中 ... return "success"; } } ``` 3. 解析Excel并插入到数据库中 在handleFileUpload方法中,可以使用Apache POI库来解析上的Excel文件,并将数据插入到数据库中。示例代码如下: ```java Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { String name = row.getCell(0).getStringCellValue(); String age = row.getCell(1).getStringCellValue(); // 将数据插入到数据库中 ... } ``` 这样就可以实现将Excel文件到数据库中了。需要注意的是,上的Excel文件需要符合一定的格式,例如第一列是姓名,第二列是年龄等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值