springboot 上传文件_Spring Boot文件上传示例(五十一)

9caeb2a793cae46bd410a99cbee279ee.png

本文介绍如何在Spring Boot Web应用程序中上传文件。

使用的工具 :

  1. Spring Boot 1.4.3.RELEASE
  2. Spring 4.3.5.RELEASE
  3. Thymeleaf
  4. Maven 3
  5. Embedded Tomcat 8.5.6

1. 项目结构

标准项目结构如下图所示 -

a861ea5945950a85f6e4da49d7f6e2f4.png

2. 项目依赖

Spring boot依赖关系,无需额外的文件上传库。

4.0.0com.felix spring-boot-file-upload jar1.0org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE1.8org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-devtools trueorg.springframework.boot spring-boot-maven-plugin 

3.文件上传示例

Spring Boot文件上传,不需要什么特别的配置。在Controller中,将上传的文件映射到MultipartFile。

文件:UploadController.java -

package com.felix.controller;import org.springframework.stereotype.Controller;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;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;@Controllerpublic class UploadController { //Save the uploaded file to this folder private static String UPLOADED_FOLDER = "D://temp//"; @GetMapping("/") public String index() { return "upload"; } @PostMapping("/upload") // //new annotation since 4.3 public String singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { if (file.isEmpty()) { redirectAttributes.addFlashAttribute("message
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值