接收上传的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
<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

  

 

转载于:https://www.cnblogs.com/MaxElephant/p/10231949.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280929.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库读写分离 - MyBatis

2019独角兽企业重金招聘Python工程师标准>>> 由于项目中数据量较大&#xff0c;访问量也较高&#xff0c;故在数据库的设计上&#xff0c;采用读写分离思想&#xff0c;达到性能要求&#xff01; 简单科普一下实现读写分离的思路 配置及加载数据库信息&#xff0c;即…

MySQL-03:数据表操作基本命令笔记

目录 数据表 1、创建表 2、删除表 3、清空表 4、修改表 5、基本数据类型 数据表 1、创建表 create table 表名(列名 类型 是否可以为空&#xff0c;列名 类型 是否可以为空 )ENGINEInnoDB DEFAULT CHARSETutf8 是否可空&#xff0c;null表示空&#xff0c;非字符串n…

java 怎么调试到第三方库的内部,在有源码的情况下

第一步&#xff0c; 把第三方库加到workspace : https://stackoverflow.com/questions/370814/how-to-set-a-breakpoint-in-eclipse-in-a-third-party-library The most sure-fire way to do this (and end up with something thats actually useful) is to download the sou…

t-mobile频段_T-Mobile再次被黑客入侵:超过200万个帐号和地址可能泄漏

t-mobile频段Attackers may have compromised three percent of T-Mobile’s 77 million customers on Monday, revealing personal information like addresses, phone numbers, and account numbers. 周一&#xff0c;攻击者可能泄露了T-Mobile 7700万客户中的3&#xff05;&…

第二篇 第三章防火防烟分区检查(一)

仓库面积可以增加3倍 就是乘以4 要一定条件 : 第二篇 第三章防火防烟分区检查&#xff08;一&#xff09; 21分钟处 该题比较有代表性 停车库 耐火等级允许最大面积 民用建筑防火分区 防烟分区的划分    防火卷帘控制器的测试 防火阀 装在通风,空调系统中 只有连在风机主管…

高斯数学

伟大的数学家高斯在9岁那年&#xff0c;用很短的时间完成了从1到100的累加。那原本是老师给学生们出的难题&#xff0c;希望他们能老老实实地待在教室里。高斯的方法很简单&#xff0c;他发现这是50个101的求和&#xff1a;100&#xff0b;1、99&#xff0b;2、98&#xff0b;3…

Ant Design Blazor 发布 0.13.0,正式支持.NET 7!

时隔3个月&#xff0c;Ant Design Blazor 发布新功能版本 0.13.0&#xff0c;并正式支持.NET 7!大家快去访问 antblazor.com 体验吧&#xff01;&#x1f525; 新增 .NET 7 目标框架支持。#2810 ElderJames&#x1f525; 重构 Mentions 组件&#xff0c;修复定位和隐藏问题。#2…

gitlab 分支操作笔记\新建远程分支\抓取远程分支\复制远程\删除分支

密码重新输入与保存 git config --global http.emptyAuth truegit config --global credential.helper store 1.不复制远程&#xff0c;直接新建远程分支。&#xff08;非正规操作&#xff09; git init //初始化 git remote add origin http://****/*****/taskboard.git…

如何在Xbox One或PlayStation 4上为Skyrim特别版安装Mods

The Elder Scrolls V: Skyrim Special Edition is now available on PlayStation 4 and Xbox One, and for the first time, “mods” are available to console gamers. Elder Scrolls V&#xff1a;Skyrim特别版现已在PlayStation 4和Xbox One上可用&#xff0c;并且首次向主…

微软宣布:PowerBI 已经与 Office 整合,一切更简单,变革又来了

很多人认为 Office 是 Office&#xff0c;PowerBI 是 PowerBI&#xff0c;怎么在 PPT 中显示 PowerBI 呢&#xff1f;这种问题以后将再不会存在。微软已经宣布&#xff0c;PowerBI 已经与 Office 深度整合&#xff0c;在未来的企业中&#xff0c;PowerBI 将与 Word&#xff0c;…

066:ORM查询条件详解-startswith和endswith:

ORM查询条件详解-startswith和endswith&#xff1a; startswith&#xff1a;判断某个字段的值是否是以某个值开始的。大小写敏感。示例代码如下&#xff1a; articles1 Article.objects.filter(title__startswith"fuck") 以上代码的意思是提取所有标题以 fuck 字符串…

前端工程师面试题汇总

HTML Doctype作用&#xff1f;严格模式与混杂模式如何区分&#xff1f;它们有何意义? HTML5 为什么只需要写 <!DOCTYPE HTML>&#xff1f; 行内元素有哪些&#xff1f;块级元素有哪些&#xff1f; 空(void)元素有那些&#xff1f; 页面导入样式时&#xff0c;使用lin…

MySQL-04:数据内容操作-增删改查-基本命令笔记

1、增 insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表 2、删 delete from 表 delete from 表 where id&#xff1d;1 and name&…

火狐和chrome_Firefox,Chrome和Edge都将支持WebAuthn的硬件两因素身份验证

火狐和chromeLogging into Gmail or Facebook could soon mean plugging in a USB device, potentially making phishing a thing of the past. 登录Gmail或Facebook可能很快就意味着要插入USB设备&#xff0c;这可能使网络钓鱼成为过去。 That’s thanks to WebAuthn, a new o…

Could not delete .........May be locked by another process.

问题 原因&#xff1a;默认的设置是文件修改后立即发布&#xff0c;这样的设置是在你每个保存文件时都会触发&#xff0c;如果tomcat已经在运行&#xff0c;这样频繁的操作也会造成文件锁死 解决&#xff1a; Tomcat 右键clean 转载于:https://www.cnblogs.com/feiZhou/p/93…

flask的基础1

1.python 现阶段三大主流web框架Django Tornado Flask的对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不管你用得到用不到,反正它全都有,属于全能型框架 2.Tornado 主要特点是原生异步非阻塞,在IO密集型应用和多任务处理上占据绝对性的优势,属…

python实现批量压缩文件夹

前段时间碰到一个需要把目录下文件夹压缩的项目&#xff0c;但是度娘里没找到&#xff0c;只好自己写脚本了。 #coding:utf-8 import os filePath raw_input("请输入路径&#xff1a;") if filePath "":os._exit() #需要退出ds list(os.walk(filePath…

单元测试01:nunit 安装与代码测试

1.nunit 下载与安装 a.下载 下载地址&#xff1a; http://nunit.org/download/ b.添加到系统环境变量 解压下载包后&#xff0c;添加两个路径到环境变量&#xff0c;如&#xff1a; D:\nunitD:\nunit\nunit-console 2.建立测试项目 a.建立class project b.project 里re…

如何将您的Google Authenticator凭证移至新的Android手机或平板电脑

Most of the app data on your Android is probably synced online will automatically sync to a new phone or tablet. However, your Google Authenticator credentials won’t — they aren’t synchronized for obvious security reasons. Android上的大多数应用程序数据可…

关于经纬度的两个计算[Teaksxgluxv]

一、子午线周长(公里) 40008.548 赤道周长(公里) 40075.704 纬度40008.548 / 360(度) 111.135 公里/度40008.548 / (360*60)(分) 1.85 公里/分40008.548 / (360*60*60)(秒) 30.87 米/秒 经度首先算相应经度位置的纬度圈长度40075.704 * cos(经度)然后方法相同&#xff0c;除…