//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. openstack-taskflow 组件记录

    [Summary] TaskFlow 是一个为了 openstack 实现的 python 库,使得执行 task 变得简单,一致,易扩展,可靠: 它能以一种声明的方式,将轻量级 task 对象的创建 ...

  2. linux下将多个ts文件合并为一个MP4文件

    1. 安装ffmpeg工具 sudo apt install ffmpeg 2. 确保所有ts文件无损坏后,确保当前目录(即存放ts文件的目录)无txt文件及mp4文件,在存放ts文件的目录下建立te ...

  3. 关于阿里云服务器Linux安装Tomcat后,外网不能访问解决方案

    这里需要提及三个方面的问题   第一个方面:Linux上启动防火墙的问题 当下比较流行的Linux镜像是CentOS,所以防火墙也随之变成了firewall,那么怎么操作这个防火墙呢?   #停止fi ...

  4. Centos7 上安装FastDFS

    Centos7 上安装 FastDFS 本文章摘抄于 风止鱼歇  博客地址:https://www.cnblogs.com/yufeng218/p/8111961.html 1.安装gcc(编译时需要 ...

  5. docker容器化python服务部署(supervisor-gunicorn-flask)

    docker容器化python服务部署(supervisor-gunicorn-flask) 本文系作者原创,转载请注明出处: https://www.cnblogs.com/further-furt ...

  6. P2607[ZJOI2008] 骑士 题解

    题目 Z 国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的 Y 国发动了一场针对 Z 国的侵略战争.战火绵延五 ...

  7. 协同合约HACKATHON 0X03;

    协同合约HACKATHON 0X03; 使用Fetch.AI技术开发一个共享行程协同合约.超过100,000个FET代币奖励. 介 绍 拼车是对你的钱包和环境都非常有益的,因此UberPool™等共享 ...

  8. [JAVA]使用字节流拷贝文件

    import java.io.*; /** * @Description: * @projectName:JavaTest * @see:PACKAGE_NAME * @author:郑晓龙 * @c ...

  9. nginx限制访问域名,禁止IP访问

    有些时候我们希望系统只能通过固定的域名访问,禁止IP或者恶意绑定的域名访问. 下面的nginx配置,假如host变量不是指定的域名,将返回403. server { listen 80; server ...

  10. 蕴含式(包含EXISTS语句的分析)

    *{ font-family: STFangSong; outline: none; } 蕴含式 一.蕴含式基础 (Ⅰ)什么是"蕴含式" 设p.q为两个命题.复合命题"如 ...