全面解析SpringBoot文件上传功能

作者:J_小浩子 时间:2023-02-26 15:55:56 

这些天忙着刷题,又怕遗忘了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 测试

全面解析SpringBoot文件上传功能

上传文件后:

全面解析SpringBoot文件上传功能

在应用的 src/main/webapp/imgupload 目录下

全面解析SpringBoot文件上传功能

6).如果上传的文件大于 1M 时,上传会报错文件太大的错误,在 application.properties 中设置上传文件的参数即可


spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb

来源:http://blog.csdn.net/change_on/article/details/59529034

标签:SpringBoot,文件上传
0
投稿

猜你喜欢

  • Java Swing组件编程之JTable表格用法实例详解

    2022-12-23 01:49:26
  • android如何获取联系人所有信息

    2021-10-24 13:24:41
  • SpringBoot整合Shiro的代码详解

    2023-10-30 10:53:31
  • C#实现带消息数的App图标

    2022-03-08 11:30:52
  • 详解Java后端优雅验证参数合法性

    2021-09-06 16:07:22
  • Android仿QQ微信实时监测网络状态

    2022-10-10 06:48:52
  • C# 常用日期时间函数(老用不熟)

    2021-08-21 10:12:18
  • 关于MyBatis模糊查询的几种实现方式

    2023-05-09 04:23:12
  • Json操作库DynamicJson使用指南

    2023-06-17 10:17:35
  • 解析Nacos的API居然存在这么严重的漏洞

    2022-05-29 08:17:52
  • Java 实现常见的非对称加密算法

    2023-11-27 18:51:03
  • Kryo框架使用方法代码示例

    2021-05-30 15:46:05
  • 解析spring cloud ouath2中的Eureka

    2023-10-12 04:07:54
  • Java编程关于子类重写父类方法问题的理解

    2022-05-25 05:00:32
  • Java maven三种仓库,本地仓库,私服,中央仓库的配置

    2023-04-13 12:05:17
  • Java基础教程之HashMap迭代删除使用方法

    2023-10-04 02:15:32
  • c#数据绑定之删除datatable数据示例

    2022-03-18 03:42:24
  • Maven 多profile及指定编译问题的解决

    2022-04-22 23:43:24
  • 利用Java实现简单的词法分析器实例代码

    2023-10-06 08:14:45
  • C#使用Matrix执行缩放的方法

    2022-05-03 15:46:58
  • asp之家 软件编程 m.aspxhome.com