博客
关于我
Spring Boot - axios upload file(带请求头上传文件,非前后端分离)
阅读量:297 次
发布时间:2019-03-03

本文共 3540 字,大约阅读时间需要 11 分钟。

文章目录

在这里插入图片描述

项目

在这里插入图片描述

新建 Spring Starter Project,编辑 pom.xml 文件,引入依赖:

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
com.mk
spring-boot-axios-upload-file
1.0.0
spring-boot-axios-upload-file
1.8
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-configuration-processor
true
org.projectlombok
lombok
true
commons-io
commons-io
2.6
org.springframework.boot
spring-boot-maven-plugin
org.springframework.boot
spring-boot-configuration-processor
org.projectlombok
lombok

编辑 application.yml 文件,设置上传文件的大小限制:

spring:  servlet:    multipart:      max-file-size: 200MB      max-request-size: 1000MB

IndexController 控制器:

package com.mk.controller;import java.io.File;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;@Controllerpublic class IndexController {       @GetMapping({   "", "/index"})    public String index() {           return "index";    }        @PostMapping("/upload")    @ResponseBody    public String upload(HttpServletRequest request,            @RequestParam(value = "file", required = false) MultipartFile file,            String filename) throws IllegalStateException, IOException {                   String authorization = request.getHeader("Authorization");        System.out.println("Authorization: " + authorization);                String originalFilename = file.getOriginalFilename();        file.transferTo(new File("G:/20191212", originalFilename));                return filename;    }}

启动类:

package com.mk;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {       public static void main(String[] args) {           SpringApplication.run(Application.class, args);    }}

src/main/resources/templates/index.html 文件:

            
Upload File

参考

转载地址:http://ohyq.baihongyu.com/

你可能感兴趣的文章
PCL MLS論文Computing and Rendering Point Set Surfaces研讀筆記
查看>>
CentOS下Nvidia docker 2.0之安裝教程&踩坑實錄
查看>>
PIL及matplotlib:OSError: cannot identify image file錯誤及解決方式
查看>>
H5页面授权获取微信授权(openId,微信nickname等信息)
查看>>
SpringBoot的URL是如何拼接的
查看>>
2018年年终总结
查看>>
解决checkbox未选中不传递value的多种方法
查看>>
【pgsql-参数详解1】PostgreSQL默认参数值
查看>>
PostgreSQL11-Hash哈希分区数量的设定标准
查看>>
HTTP协议(1)_入门的一些教程和资源
查看>>
2021年春季ACM训练赛第3场
查看>>
Go-常用命令go的使用(build、env、run、fmt等)
查看>>
钉钉登录及常用的URL及IP
查看>>
CENTOS 删除nginx
查看>>
【redis键过期删除策略】很高兴再次认识你
查看>>
【工具篇】EasyExcel的应用
查看>>
SSM发送手机验证码——以网建SMS为例
查看>>
大范围卫星影像快速处理
查看>>
监控264后缀文件播放
查看>>
网站在线偷拍照片源码
查看>>