Springboot文件上传

 1.创建Springboot项目,结构如下:       

      

 

 2. 代码部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
 
//文件上传
@Controller
@RequestMapping("upload")
public class UploadConcoller {
 
    @RequestMapping("test")
    public String upload(MultipartFile fileTest, HttpServletRequest request) throws IOException {
        //获取上传的文件名
        System.out.println("上传的文件名为:"+fileTest.getOriginalFilename());
        System.out.println("上传的文件大小为:"+fileTest.getSize());
        System.out.println("上传的文件类型为:"+fileTest.getContentType());
 
 
        //动态获取路径
        String realPath = request.getSession(true).getServletContext().getRealPath("upload");
 
        //保证上传的文件名唯一
        String uu = UUID.randomUUID().toString().replace("-","");
        String newFile = uu+fileTest.getOriginalFilename();
        System.out.println("新名字:"+newFile);
 
        //创建新的文件夹--当前系统时间
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        System.out.println("文件名:"+format);
        new File(realPath,format).mkdir();
        //复制到该目录下
        fileTest.transferTo(new File(realPath+"/"+format,newFile));
 
        return "redirect:/index.jsp";
    }
 
}

 3. index.jsp页面

1
2
3
4
5
6
7
<%@ page pageEncoding="UTF-8" isELIgnored="false"  %>
 
<form action="${pageContext.request.contextPath}/upload/test" method="post"  enctype="multipart/form-data">
    <input type="file" name="fileTest"></br>
    </br>
    <input type="submit" value="提交">
</form>

 4. pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!--父级项目依赖-->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.5.RELEASE</version>
</parent>
 
<dependencies>
 
  <!--web支持的jar springboot的启动器-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--JSP解析依赖-->
  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
  </dependency>
 
 
  <!--war包-->
  <!--去掉内嵌tomcat-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
  </dependency>
 
  <!--去掉使用内嵌tomcat解析jsp-->
  <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
  </dependency>
 
</dependencies>

5. applcation.properties

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

#上传时出现异常:  上传文件的大小超出默认配置  默认10M
#用来指定服务端最大文件大小
spring.servlet.multipart.max-file-size=50MB
#用来控制文件上传大小的限制
spring.servlet.multipart.max-request-size=60MB 

6.启动项目,访问项目接口 会跳转到 jsp页面  

  

 

 
posted @   向大海  阅读(629)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示