SpringBoot实现文件上传


开发准备:        1.快速搭建一个springboot项目     2.准备上传页面  提供form表单  post提交  multipart/form-data     3.开发控制器 使用MultipartFile形式接收接收的变量名必须和上传页面的name一至  将接收到的文件放入到执行目录中即可   pom.xml文件引入的依赖:
<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.3.0.RELEASEversion>
    <relativePath/> 
  parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-testartifactId>
      <scope>testscope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintagegroupId>
          <artifactId>junit-vintage-engineartifactId>
        exclusion>
      exclusions>
    dependency>

    
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-tomcatartifactId>
    dependency>
    <dependency>
      <groupId>org.apache.tomcat.embedgroupId>
      <artifactId>tomcat-embed-jasperartifactId>
    dependency>

    
    <dependency>
      <groupId>commons-fileuploadgroupId>
      <artifactId>commons-fileuploadartifactId>
      <version>1.3.2version>
    dependency>

  dependencies>
  jsp页面代码:
 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>文件上传页面title>
 5 head>
 6 <body>
 7      <center>
 8          <form action="${pageContext.request.contextPath}/fc/upload" method="post" enctype="multipart/form-data">
 9             <input type="file" name="aaa">
10             <input type="submit" value="上传">
11          form>
12      center>
13 body>
14 html>

Controller代码

 1 package com.liusha.ems.controller;
 2 
 3 import org.apache.commons.io.FilenameUtils;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.multipart.MultipartFile;
 7 
 8 import javax.servlet.http.HttpServletRequest;
 9 import java.io.File;
10 import java.io.IOException;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.UUID;
14 
15 @Controller
16 @RequestMapping("/fc")
17 public class FileController {
18 
19     @RequestMapping("/getFile")
20     public String getFile(){
21         return "/upload";
22     }
23 
24     //处理文件上传的方法
25     @RequestMapping("/upload")
26     public String upload(MultipartFile aaa, HttpServletRequest request) throws IOException {
27 
28         //根据相对获取绝对路径(文件上传到的保存位置)
29         String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/files");
30         //获取文件后缀
31         String extension = FilenameUtils.getExtension(aaa.getOriginalFilename());
32         //文件名我这里使用UUID和时间组成的
33         String newFileNamePrefix= UUID.randomUUID().toString().replace("-","")+
34                 new SimpleDateFormat("yyyyMMddHHssSSS").format(new Date());
35         String newFileName=newFileNamePrefix+"."+extension;
36         //处理文件上传
37         aaa.transferTo(new File(realPath,newFileName));
38 
39         //上传成功后跳转到的路径
40         return "redirect:/fc/getFile";
41     }
42 
43 }