码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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();
        }
    }
    
    
  • 相关阅读:
    java毕业设计汽车租赁系统Mybatis+系统+数据库+调试部署
    RabbitMQ的基本介绍
    Docker的使用
    返回二叉树中最大的二叉搜索子树的大小
    【Spring源码】12. 注册bean处理器registerBeanPostProcessors()
    【C语言】整理姓名表。将输入的若干姓名按字典顺序排序并输出。
    【算法训练-链表 三】【判断】判断链表中是否有环、判断链表是否为回文链表
    NetSuite税额尾差的处理
    【Leetcode每日一题】 动态规划 - 简单多状态 dp 问题 - 按摩师(难度⭐)(61)
    【自然语言处理】【多模态】OFA:通过简单的sequence-to-sequence学习框架统一架构、任务和模态
  • 原文地址:https://blog.csdn.net/weixin_45732391/article/details/140412582
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号