SPRING BOOT FILE UPLOAD 文件上传

原文链接:http://www.devnp.com/2018/07/30/spring-boot-file-upload-%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/

参考链接:https://www.cnblogs.com/KKSJS/p/9622817.html

 

Spring Boot File Upload 文件上传和Sping MVC文件上传类似:Spring MVC File Upload 文件的上传

本示例以Spring Boot 2.0.3.RELEASE 为例来演示文件的上传,系统环境使用Windows,注意路径

1. 生成项目

打开 https://start.spring.io/ 选择版本,所需要的依赖和相关信息,然后点击生成项目。

导入到Eclipse中:

2. 编写上传代码

处理文件上传的控制器,包含单一文件上传和多个文件上传:

FileUploadController.java

01

02

03

04

05

06

07

08

09

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

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

package com.devnp.springbootfileuploaddemo;

 

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.util.FileCopyUtils;

import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;

 

@Controller

public class FileUploadController {

 

    @GetMapping("/")

    public String listUploadedFiles(Model model) throws IOException {

         

        return "uploadForm" ;

    }

     

     

    @PostMapping("/upload")

    public String upload(@RequestParam("avatar") MultipartFile file,

            RedirectAttributes redirectAttributes, Model model) throws IOException {

         

        List<String> msg = new ArrayList<>();

         

        FileCopyUtils.copy(file.getBytes(), new File("D:/" + file.getOriginalFilename()));

         

        msg.add("You successfully uploaded " + file.getOriginalFilename() + "!");

         

        model.addAttribute("messages", msg);

         

        return "uploadMessage" ;

    }

     

    @PostMapping("/uploads")

    public String upload(@RequestParam("files") MultipartFile[] files, Model model) throws IOException {

         

        List<String> msg = new ArrayList<>();

         

        for(int i = 0; i< files.length; i++ ) {

             

            if(!StringUtils.isEmpty(files[i].getOriginalFilename())) {

                FileCopyUtils.copy(files[i].getBytes(), new File("D:/" + files[i].getOriginalFilename()));

             

                msg.add("You successfully uploaded " + files[i].getOriginalFilename() + "!");

            }

        }

         

        model.addAttribute("messages", msg);

         

        return "uploadMessage" ;

    }

}

上传页面:
uploadForm.html

01

02

03

04

05

06

07

08

09

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

43

44

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8" />

    <title>File Upload</title>

</head>

<body>

    <div>Single File Upload</div>

    <form method="POST" enctype="multipart/form-data" action="/upload">

        <table>

            <tr>

                <td>File to upload:</td>

                <td><input type="file" name="avatar" /></td>

            </tr>

            <tr>

                <td></td>

                <td><input type="submit" value="Upload" /></td>

            </tr>

        </table>

    </form>

     

    <div>Multiple Files Upload</div>

    <form method="POST" enctype="multipart/form-data" action="/uploads">

        <table>

            <tr>

                <td>File to upload:</td>

                <td><input type="file" name="files" /></td>

            </tr>

            <tr>

                <td>File to upload:</td>

                <td><input type="file" name="files" /></td>

            </tr>

            <tr>

                <td>File to upload:</td>

                <td><input type="file" name="files" /></td>

            </tr>

            <tr>

                <td></td>

                <td><input type="submit" value="Upload" /></td>

            </tr>

        </table>

    </form>

</body>

</html>

当文件上传成功时会展示上传的文件名称, 所以展示页面:
uploadMessage.html

01

02

03

04

05

06

07

08

09

10

11

12

13

14

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<meta charset="UTF-8" />

<title>File Upload Message</title>

</head>

<body>

    <div>

        <ul>

            <li th:each="msg : ${messages}" th:text="${msg}"></li>

        </ul>

    </div>

</body>

</html>

3. 测试

启动项目,然后在浏览器输入:http://localhost:8080

 

4. 配置

在Spring Boot中,可以使用配置对文件上传进行设置:

1

2

3

4

5

6

7

# MULTIPART (MultipartProperties)

spring.servlet.multipart.enabled=true # 是否允许多个文件上传

spring.servlet.multipart.file-size-threshold=0 # 文件写入磁盘后的阈值。 值可以使用后缀“MB”或“KB”分别表示兆字节或千字节。

spring.servlet.multipart.location= # 上传文件的中间位置。

spring.servlet.multipart.max-file-size=1MB # 上传单个文件 最大值 可以以 "MB" 或者 "KB"作为单位

spring.servlet.multipart.max-request-size=10MB # 上传文件的总数和的最大值 可以以 "MB" 或者 "KB"作为单位

spring.servlet.multipart.resolve-lazily=false # 是否在文件或参数访问时懒惰地解决多部分请求。

配置这些设置之后,如果出现不符合的情况,spring则会抛出异常,可以应该捕获这些异常来完成对应的相应。

5. 代码

spring-boot-file-upload-demo.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值