//vue element-ui组件
<el-upload style="position: relative;top: -40px;left: 240px;"
                      :show-file-list="false"
                      :on-success="onSuccess"
                      :on-error="onError"
                      :before-upload="beforeUpload"
                      :on-remove="handleRemove"
                      action="/api/upLoadFile"
                      accept=".json, .txt, .csv, .xls, .xlsx"
                    >
                      <el-button plain type="primary">导入</el-button>
                    </el-upload>
//js方法
  handleRemove(file, fileList) {
    },
        /**
     * 上传之前回调函数
     */
    beforeUpload(file) {
      console.log(file.name);
      this.uploaDialog = true;
    },
      /**
     * 上传失败回调函数
     */
    onError(err, file, fileList) {
      this.enabledUploadBtn = false;
      this.$message({
        message: "上传失败",
        type: "error"
      });
    },
    /**
     * 上传成功回调函数
     */
    onSuccess(response, file, fileList) {
      this.enabledUploadBtn = false;
      // console.log(response)
      this.$message({
        message: response.msg,
          type: "info"
      });
        file = [];
        fileList = [];
    },

//后台

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadFile {

   private static Logger log = LoggerFactory.getLogger(UploadFile.class);

  @RequestMapping(value = "/upLoadFile", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
  @ResponseBody
  public String importFile(@RequestParam("file") MultipartFile file,
    HttpServletRequest request){
      log.info("importFile()");
      Map<String, String> reMap = new HashMap<>();
      if (file.isEmpty()) {
        return "文件为空";
      }
      // 获取文件名
      String fileName = file.getOriginalFilename();
      log.info("上传的文件名为:" + fileName);
      // 获取文件的后缀名
      String suffixName = fileName.substring(fileName.lastIndexOf("."));
      log.info("上传的后缀名为:" + suffixName);
      // 文件上传路径 应该改为服务器路径
      String filePath = "f:/objdocument/";
      File dest = new File(filePath + fileName);
      // 检测是否存在目录
      if (!dest.getParentFile().exists()) {
        dest.getParentFile().mkdirs();
      }
      try {
        file.transferTo(dest);
        reMap.put("msg", "上传成功");
      } catch (IllegalStateException e) {
        log.info(e.toString());
        reMap.put("msg", "上传失败");
      } catch (IOException e) {
        log.info(e.toString());
        reMap.put("msg", "上传失败");
      }
      String str = JsonUtil.map2Json(reMap);
      return str;
  }
}

application.properties相关配置

spring.http.multipart.enabled=true #默认支持文件上传.
spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘.
spring.http.multipart.location= # 上传文件的临时目录
spring.http.multipart.max-file-size=1Mb # 最大支持文件大小
spring.http.multipart.max-request-size=10Mb # 最大支持请求大小

vue+springboot文件上传的更多相关文章

  1. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  2. 【SpringBoot】07.SpringBoot文件上传

    SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...

  3. springboot+vue实现文件上传

    https://blog.csdn.net/mqingo/article/details/84869841 技术: 后端:springboot 前端框架:vue 数据库:mysql pom.xml: ...

  4. SpringBoot 文件上传临时文件路径问题

    年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...

  5. springboot文件上传下载简单使用

    springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...

  6. SpringBoot文件上传(MVC情况和webFlux情况)

    MVC情况 引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  7. springboot 文件上传大小配置

    转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...

  8. SpringBoot文件上传下载

    项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...

  9. Springboot 文件上传(带进度条)

    1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

随机推荐

  1. Vue数据更新页面没有更新问题总结

    Vue数据更新页面没有更新问题总结 1. Vue无法检测实例别创建时不存在于data中的property 原因: 由于Vue会在初始化实例时对property执行getter/setter转化,所以p ...

  2. Distributed Runtime

    上级:https://www.cnblogs.com/hackerxiaoyon/p/12747387.html Tasks and Operator Chains 任务和操作链 对于分布式执行器,f ...

  3. 重学 Java 设计模式:实战中介者模式「按照Mybaits原理手写ORM框架,给JDBC方式操作数据库增加中介者场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 同龄人的差距是从什么时候拉开的 同样的幼儿园.同样的小学.一样 ...

  4. 如何在 asp.net core 3.x 的 startup.cs 文件中获取注入的服务

    一.前言 从 18 年开始接触 .NET Core 开始,在私底下.工作中也开始慢慢从传统的 mvc 前后端一把梭,开始转向 web api + vue,之前自己有个半成品的 asp.net core ...

  5. C# 特性篇 Attributes

    特性[Required] (必修的) /// <summary> /// 操作人EmpID /// </summary> [Required] public string Op ...

  6. wsl环境下配置ubuntu16.04

    wsl环境下配置ubuntu16.04 在公司同事的安利下,终于给自己用了8年的老笔记本(戴尔XPS L502X)换上了固态硬盘(WD500G,SATA3接口) 当然,系统重装了一遍,所有的软件也都没 ...

  7. 哎,老了之display-box

    哎,不想吐槽自己了,表示已远远落后,从今天起开始恶补吧,来一个实例 <html> <head> <meta name="generator" cont ...

  8. SpringCloud系列之集成分布式事务Seata应用篇

    目录 前言 项目版本 项目说明 Seata服务端部署 Seata客户端集成 cloud-web module-order module-cart module-goods module-wallet ...

  9. Vue动态修改class

    #####对象方法-最简单的绑定(这里的active加不加单引号都可以,以下也一样都能渲染) :class="{ 'active': isActive }"1判断是否绑定一个act ...

  10. CentOS7下安装Docker《超详细新手教程》

    1.使用 root 权限登录 Centos.确保 yum 包更新到最新. sudo yum update 2.卸载旧版本(如果安装过旧版本的话) sudo yum remove docker dock ...