
Spring Boot - axios upload file(带请求头上传文件,非前后端分离)
发布日期:2021-05-06 23:03:04
浏览次数:7
分类:技术文章
本文共 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
参考
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年03月30日 22时30分50秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
vue-router路由 学习笔记
2019-03-03
【数据库】第七章课后题
2019-03-03
第四章 串、数组和广义表 —— BF算法和KMP算法
2019-03-03
[选拔赛1]花园(矩阵快速幂),JM的月亮神树(最短路),保护出题人(斜率优化)
2019-03-03
DLA:一种深度网络特征融合方法
2019-03-03
leetcode114(二叉树展开为链表)
2019-03-03
java —— static 关键字
2019-03-03
在 Python 调试过程中设置不中断的断点 | Linux 中国
2019-03-03
使用开源可视化工具来理解你的 Python 代码 | Linux 中国
2019-03-03
硬核观察 | 有人在比特币骗局中损失了 10 个比特币
2019-03-03
使用 top 命令了解 Fedora 的内存使用情况 | Linux 中国
2019-03-03
8皇后问题 递归 函数调用是重点
2019-03-03
1541 +1 *2 ²
2019-03-03
面试别慌!阿里专家带你从【入门+基础+进阶+项目】攻破SpringBoot
2019-03-03
【Java面试】30个 Java 集合面试必备的问题和答案
2019-03-03
华为鸿蒙到底是不是安卓系统套了个壳?
2019-03-03
fragment中recyclerview的重新加载问题
2019-03-03
window程序设计(1):第一个windows程序
2019-03-03
windows程序设计(4):文本输出
2019-03-03