服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - 全面解析SpringBoot文件上传功能

全面解析SpringBoot文件上传功能

2021-02-06 12:26J_小浩子 JAVA教程

这篇文章主要为大家全面解析SpringBoot文件上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这些天忙着刷题,又怕遗忘了spring boot, 所以抽出一点时间折腾折腾,加深点印象。
spring boot 的文件上传与 spring mvc 的文件上传基本一致,只需注意一些配置即可。
环境要求: spring boot v1.5.1.release + jdk1.7 + myeclipse

1).引入thymeleaf,支持页面跳转

?
1
2
3
4
5
<!-- 添加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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!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 中写两个方法,一个方法跳转到上传文件的页面,一个方法处理上传文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//跳转到上传文件的页面
  @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 方法中

?
1
2
3
4
5
6
7
8
9
10
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 中设置上传文件的参数即可

?
1
2
spring.http.multipart.maxfilesize=100mb
spring.http.multipart.maxrequestsize=100mb

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/change_on/article/details/59529034

延伸 · 阅读

精彩推荐