//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. jQuery实现全选、反选、删除

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  2. SQL注入之注入点的寻找

    注入点的判断 判断一个链接是否存在注入漏洞,可以通过对其传入的参数(但不仅仅只限于参数,还有cookie注入,HTTP头注入等) 进行构造,然后对服务器返回的内容进行判断来查看是否存在注入点. 注入点 ...

  3. StringBuider 在什么条件下、如何使用效率更高?

    声明:本文首发于博客园,作者:后青春期的Keats:地址:https://www.cnblogs.com/keatsCoder/ 转载请注明,谢谢! 引言 都说 StringBuilder 在处理字符 ...

  4. 收藏python开发各种资源官方文档

    http://json.cn/ https://cn.bing.com/ https://processon.com/ https://docs.djangoproject.com/en/1.11/r ...

  5. jQurey Select2 4.0

    https://jeesite.gitee.io/front/jquery-select2/4.0/index.htm

  6. Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析

    一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...

  7. a标签绑定点击事件失败

    如图 然后对a标签绑定点击事件  无效 换成span标签可以

  8. 状压DP之Bill的挑战

    题目 P2167 [SDOI2009]Bill的挑战 Sheng bill不仅有惊人的心算能力,还可以轻松地完成各种统计.在昨天的比赛中,你凭借优秀的程序与他打成了平局,这导致Sheng bill极度 ...

  9. CodeFoeces 1215 D Ticket Game(数学,思维)

    CodeFoeces 1215 D Ticket Game 题目大意 M和B轮流玩游戏(每一轮M先手 现在给出一个长度为偶数的串,包含字符'?'和数字 现在两人依次在'?'上填数字\(0\)~\(9\ ...

  10. like's photos

    wallhaven官网