用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

公园    -  云代码空间

——

企业级 SpringBoot 教程 (十七)上传文件

2019-03-07|546阅||

摘要:企业级 SpringBoot 教程 (十七)上传文件

这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件。

构建工程

为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖。为例能够上传文件在服务器,你需要在web.xml中加入标签做相关的配置,但在sringboot 工程中,它已经为你自动做了,所以不需要你做任何的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>


创建文件上传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
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
@Controller
public class FileUploadController {
 
    private final StorageService storageService;
 
    @Autowired
    public FileUploadController(StorageService storageService) {
        this.storageService = storageService;
    }
 
    @GetMapping("/")
    public String listUploadedFiles(Model model) throws IOException {
 
        model.addAttribute("files", storageService
                .loadAll()
                .map(path ->
                        MvcUriComponentsBuilder
                                .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
                                .build().toString())
                .collect(Collectors.toList()));
 
        return "uploadForm";
    }
 
    @GetMapping("/files/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
 
        Resource file = storageService.loadAsResource(filename);
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFilename()+"\"")
                .body(file);
    }
 
    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {
 
        storageService.store(file);
        redirectAttributes.addFlashAttribute("message",
                "You successfully uploaded " + file.getOriginalFilename() + "!");
 
        return "redirect:/";
    }
 
    @ExceptionHandler(StorageFileNotFoundException.class)
    public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
        return ResponseEntity.notFound().build();
    }
 
}


这个类通过@Controller注解,表明自己上一个Spring mvc的c。每个方法通过 
@GetMapping 或者@PostMapping注解表明自己的 http方法。

  • GET / 获取已经上传的文件列表
  • GET /files/{filename} 下载已经存在于服务器的文件
  • POST / 上传文件给服务器

创建一个简单的 html模板

为了展示上传文件的过程,我们做一个界面: 
在src/main/resources/templates/uploadForm.html

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
<html xmlns:th="http://www.thymeleaf.org">
<body>
 
    <div th:if="${message}">
        <h2 th:text="${message}"/>
    </div>
 
    <div>
        <form method="POST" enctype="multipart/form-data" action="/">
            <table>
                <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
                <tr><td></td><td><input type="submit" value="Upload" /></td></tr>
            </table>
        </form>
    </div>
 
    <div>
        <ul>
            <li th:each="file : ${files}">
                <a th:href="${file}" th:text="${file}" />
            </li>
        </ul>
    </div>
 
</body>
</html>


上传文件大小限制

如果需要限制上传文件的大小也很简单,只需要在springboot 工程的src/main/resources/application.properties 加入以下:

1
2
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB


架构代码如下 :

"分布式b2b <wbr

 

资料和源码来源地址


Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求:yuncode.net
顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 公园
    • 等级: 初级设计师
    • 积分: 2760
    • 代码: 0 个
    • 文章: 89 篇
    • 随想: 0 条
    • 访问: 10 次
    • 关注

    人气代码

      标签

      MVC(9)教程(4)Spring(5)Redis分布式缓存(1)spring,spr(1)mvc,web开发,(1)spring,spr(1)mvc,web开发,(1)Zookeeper集(1)restful,(2)kafka,(2)shiro,Spri(1)MVC,mybati(1)跟我学习dubbo,(1)J2ee分布式架构,(1)shiro(1)跟我学习dubbo-(1)跟我学习dubbo-(1)spring+spr(1)Spring4+Sp(1)DUBBO与ZOOK(1)【分享】微服务分布式(1)Springmvc+(1)JEESZ(1)RestFul服务介(1)J2EE分布式架构(3)dubbo+spri(1)基于redis分布式(1)SSM框架Sprin(1)分布式缓存Redis(1)Centos下单节点(1)JEESZ分布式框架(1)JEESZ分布式框架(1)分布式架构sprin(1)+mybatis(8)+shiro+(3)Activiti(3)分布式服务:spri(1)+(11)Dubbo+Zook(1)JEESZ-kafk(1)JEESZ-Zook(1)FastDFS安装、(1)FastDFS分布式(1)分布式服务:spri(1)Dubbo+Zook(1)分布式架构sprin(1)FastDFS分布式(1)FastDFS安装、(1)Maven启用代理访(1)如何从Maven远程(1)Maven安装配置(1)Maven本地资源库(1)使用Maven创建J(1)使用Maven创建W(1)Maven(2)POM(1)构建生命周期(1)SSM框架——详细整(1)JavaEE的13种(1)使用Maven构建和(1)Maven存储库(1)(一)构建dubbo(1)(二)构建dubbo(1)(三)构建dubbo(1)分布式框架简介SSM(1)springmvc+(1)Maven快照(1)Maven项目模板(1)Maven构建自动化(1)dubbo(4)springmvc(4)mybatis(2)java企业架构(1)SSM框架——Spr(1)分布式服务:spri(1)Dubbo+Zook(1)分布式服务:spri(1)Dubbo+Zook(1)springmvc+(1)分布式架构sprin(1)mvc配置(2)dbcp数据源+jd(1)详细介绍(1)(十三)(1)(十四)(1)(十五)(1)MVC原理(1)入门示例讲解(1)(十六)(1)(十七)(1)Springmvc+(1)j2ee分布式架构核(1)【分享】微服务分布式(1)Springmvc+(1)dbcp数据源+jd(1)DUBBO+SPRI(1)JEESZ分布式系统(1)构建dubbo分布式(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)构建dubbo分布式(1)Springmvc+(1)构建springmv(1)构建springmv(1)构建dubbo分布式(1)构建dubbo分布式(1)跟我学习dubbo-(1)构建dubbo分布式(1)构建dubbo分布式(1)【企业级框架整合】S(1)构建dubbo分布式(1)【推荐】微服务大型分(1)springmvc整(1)【分享】微服务分布式(1)Springmvc+(1)SpringBoot(4)企业级(4)(十七)上传文件(1)(十四)在sprin(1)(十五)Spring(1)(十六)用restT(1)

      站长推荐