全面解析SpringBoot文件上传功能
作者:J_小浩子 发布时间:2023-02-26 15:55:56
标签:SpringBoot,文件上传
这些天忙着刷题,又怕遗忘了spring boot, 所以抽出一点时间折腾折腾,加深点印象。
spring boot 的文件上传与 spring mvc 的文件上传基本一致,只需注意一些配置即可。
环境要求: Spring Boot v1.5.1.RELEASE + jdk1.7 + myeclipse
1).引入thymeleaf,支持页面跳转
<!-- 添加thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2).在 src/main/resources 目录下新建 static 目录和 templates 目录。 static存放静态文件,比如 css、js、image… templates 存放静态页面。先在templates 中新建一个 uploadimg.html
<!DOCTYPE html>
<html>
<head>
<title>uploadimg.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3"></meta>
<meta name="description" content="this is my page"></meta>
<meta name="content-type" content="text/html; charset=UTF-8"></meta>
<!--<link rel="stylesheet" type="text/css" href="./styles.css" rel="external nofollow" >-->
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/testuploadimg">
图片<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
3).在 controller 中写两个方法,一个方法跳转到上传文件的页面,一个方法处理上传文件
//跳转到上传文件的页面
@RequestMapping(value="/gouploadimg", method = RequestMethod.GET)
public String goUploadImg() {
//跳转到 templates 目录下的 uploadimg.html
return "uploadimg";
}
//处理文件上传
@RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
public @ResponseBody String uploadImg(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
String contentType = file.getContentType();
String fileName = file.getOriginalFilename();
/*System.out.println("fileName-->" + fileName);
System.out.println("getContentType-->" + contentType);*/
String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
try {
FileUtil.uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
// TODO: handle exception
}
//返回json
return "uploadimg success";
}
4).在上面中,我将文件上传的实现写在工具类 FileUtil 的 uploadFile 方法中
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}
5).在浏览器输入 :http://localhost:8080/gouploadimg 测试
上传文件后:
在应用的 src/main/webapp/imgupload 目录下
6).如果上传的文件大于 1M 时,上传会报错文件太大的错误,在 application.properties 中设置上传文件的参数即可
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb
来源:http://blog.csdn.net/change_on/article/details/59529034
0
投稿
猜你喜欢
- maven 常见命令配置maven常用命令#创建项目 -D设置参数mvn archetype:generate -DgroupId=cn.d
- 如何在原有日期时间上加几个月或几天在原有的时间上添加几个月SimpleDateFormat df = new SimpleDateForma
- 树的结构说得差不多了,现在我们来说说一种数据结构叫做哈希表(hash table),哈希表有是干什么用的呢?我们知道树的操作的时间复杂度通常
- ArrayList集合在查询元素时速度很快,但在增删元素时效率较低,为了克服这种局限性,可以使用List接口的另一个实现类LinkedLis
- 前言在项目中,如果我们要遵循分层领域模型规约: 话,肯定避免不了在DTO、VO、BO、AO、VO、Query等实体的转换,我们通常有几种做法
- 递归方法定义本身调用方法本身的现象叫做递归在这之前我们学的东西:例如StringBuffer.append().append().appen
- 之前看过一句话,说的特别好。有人问阅读源码有什么用?学习别人实现某个功能的设计思路,提高自己的编程水平。是的,大家都实现一个功能,不同的人有
- 前言古语有云:道为术之灵,术为道之体;以道统术,以术得道。其中:“道”指“规律、道理、理论”,“术”指“方法、技巧、技术”。意思是:“道”是
- 利用java8流的特性,我们可以实现list中多个元素的 属性求和 并返回。案例:有一个借款待还信息列表,其中每一个借款合同包括:本金、手续
- Jenkins集成Sonar过程中遇到的报错1、jenkins中无法添加sonarqube的token凭证因为添加的凭证类型错误,所以无法添
- 前言微服务架构,前后端分离目前已成为互联网项目开发的业界标准,其核心思想就是前端(APP、小程序、H5页面等)通过调用后端的API接口,提交
- 最近同事问我有没有有关于技术的电子书,我打开电脑上的小书库,但是邮件发给他太大了,公司又禁止用文件夹共享,于是花半天时间写了个小的文件上传程
- 1、继承Thread类方式这种方式适用于执行特定任务,并且需要获取处理后的数据的场景。举例:一个用于累加数组内数据的和的线程。public
- 一、引言在许多编程语言中,都有函数回调这一概念。C 和 C++ 中有函数指针,因此可以将函数作为参数传给其它函数,以便过后调用。而在 Jav
- 在传统的Java编程中,被广为人知的一个知识点是:java Interface接口中不能定义private私有方法。只允许我们定义publi
- 一、SpringBoot 指定配置文件路径:在 SpringBoot 中,可以将配置文件放在 jar 包外面,这样可以方便地修改配置而不需要
- 1. matlab的lp2lp函数的作用去归一化 H(s) 的分母2. matlab的lp2lp函数的使用方法[z, p, k]=butta
- 在Mybatis的动态SQL和${}形式的参数中都用到了OGNL表达式。Mybatis常用的OGNL表达式如下1、e1 or e2:或<
- public class CrossSum{ public static void main(String args[]){
- 简介由于最近的项目需求,需要在把配置类导入到容器中,通过查询,使用@Import注解就能实现这个功能,@Import注解能够帮我们吧普通配置