欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

全面解析SpringBoot文件上传功能

时间:2021-05-19 09:41:36|栏目:JAVA代码|点击:

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

上一篇:Springboot+echarts实现可视化

栏    目:JAVA代码

下一篇:spring batch 读取多个文件数据导入数据库示例

本文标题:全面解析SpringBoot文件上传功能

本文地址:http://www.codeinn.net/misctech/124631.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有