项目结构:

1、单文件上传

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>upload file</title>
</head>
<body>
<form action="/uploadfile/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>

http://localhost:8080/uploadfile/upload

package com.archibladwitwicke.springboot2.chapter03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import java.io.File; @Controller
@RequestMapping("/uploadfile")
public class UploadFileController { @RequestMapping("/upload")
public String upload() {
return "/upload.html";
} @PostMapping("/upload")
@ResponseBody
public String upload(MultipartFile file) {
if (file.isEmpty()) {
return "file is null";
} String originFileName = file.getOriginalFilename();
String destFileLocation = "C:\\Users\\Administrator\\Desktop\\" + originFileName;
File destFile = new File(destFileLocation);
try {
file.transferTo(destFile);
return "upload ok! upload file location: " + destFileLocation;
} catch (Exception ex) {
return "upload false! reason: " + ex.getMessage();
}
} @RequestMapping("/uploads")
public String uploads() {
return "/multiupload.html";
}
}

2、多文件上传

multiupload.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>upload file</title>
</head>
<body>
<form action="/uploadfile/uploads" method="post" enctype="multipart/form-data">
<input type="file" name="files"><br/>
<input type="file" name="files"><br/>
<input type="file" name="files"><br/>
<input type="submit" value="上传">
</form>
</body>
</html>

http://localhost:8080/uploadfile/uploads

package com.archibladwitwicke.springboot2.chapter03.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import java.io.File; @Controller
@RequestMapping("/uploadfile")
public class UploadFileController { @RequestMapping("/uploads")
public String uploads() {
return "/multiupload.html";
} @PostMapping("/uploads")
@ResponseBody
public String uploads(MultipartFile[] files) { String result = null; if (files.length == 0) {
return "file is null";
} for (MultipartFile file : files) {
String originFileName = file.getOriginalFilename();
String destFileLocation = "C:\\Users\\Administrator\\Desktop\\" + originFileName;
File destFile = new File(destFileLocation);
try {
file.transferTo(destFile);
result += "upload ok! upload file location: " + destFileLocation;
} catch (Exception ex) {
result = "upload false! reason: " + ex.getMessage();
}
} return result;
}
}

SpringBoot2 上传文件 上传多文件的更多相关文章

  1. SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...

  2. SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制

    SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制 前言 LZ最近升级SpringBoo框架到2.1.6,踩了一些坑,这里介绍的是文件上传大小限制. 升级前 #文件上传配置 ...

  3. SpringBoot2.0(三) 文件上传

    SpringBoot中发起文件上传示例: /** * 文件上传 * @param multipartFile * @param path * @return */ @RequestMapping(va ...

  4. SpringBoot2.x设置文件上传文件的大小

    The field file exceeds its maximum permitted size of 1048576 bytes spring: # 设置文件上传文件大小 servlet: mul ...

  5. springboot解决文件上传大小限制

    (1)在配置文件(application.properties)加入如下代码 springboot2.0以下配置为: spring.http.multipart.maxFileSize = 10Mb ...

  6. 使用java的MultipartFile实现layui官网文件上传实现全部示例,java文件上传

    layui(谐音:类UI) 是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与组织形式,门槛极低,拿来即用. layui文件上传示例地址:https://www. ...

  7. SpringBoot整合WEB开发--(三)文件上传

    文件上传: Java中文件上传一共涉及到两个组件,CommonsMultipartResolver和StandardServletMultipartResolver,其中CommonsMultipar ...

  8. 【SpringBoot】07.SpringBoot文件上传

    SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...

  9. 十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑

    SpringBoot-基于Yml配置方式,实现文件上传逻辑 1.文件上传 2.搭建文件上传界面 2.1 引入页面模板Jar包 2.2 编写简单的上传页面 2.3 配置页面入口 3.SpringBoot ...

随机推荐

  1. CentOS中安装JAVA环境

    好消息, 程序员专用早餐机.和掌柜说 ideaam,可以节省20元. 点击链接   或復·制这段描述¥k3MbbVKccMU¥后到淘♂寳♀ 一般情况下,我们都要将linux自带的OPENJDK卸载掉, ...

  2. jQueryUI modal dialog does not show close button (x) JQueryUI和BootStrap混用时候,右上角关闭按钮显示不出图标的解决办法

    I had this problem and was able to resolve it with the declaration below. $.fn.bootstrapBtn = $.fn.b ...

  3. node 的安装

    安装方法来自于 https://nodejs.org/en/download/package-manager/ Installing Node.js via package manager Note: ...

  4. Intellij IDEA 导入Eclipse的Web项目

    Project Structure -> Modules -> Web (Module使用web技术)

  5. linux分区详解

    点评:如果你想了解linux分区,请务必认真阅读本文.对于初学者来说,linux分区不像windows下那么一目了解.    Linux 分区的规定      1. 设备管理 在 Linux 中,每一 ...

  6. Atitti dbutil获取多个返回结果集的解决

    Atitti dbutil获取多个返回结果集的解决 1.1. 多个select默认只返回第一个resultset1 1.2. 调用存储过程,也是返回第一个select的1 1.3. 如果insert前 ...

  7. 黑马程序猿————Java基础日常笔记---反射与正則表達式

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 黑马程序猿----Java基础日常笔记---反射与正則表達式 1.1反射 反射的理解和作用: 首 ...

  8. Cloud Resource

    Cloud Public Cloud Aliyun AWS Azure Cloud Stack SDN指南 DNS:Band Private Cloud DC/OS OpenStack Hybrid ...

  9. [SQL Server 2014] 微软将于年底发布新版数据库SQL Server 2014

    在今年的TechEd大会上,微软宣布SQL Server 2014的第一个技术预览版.SQL Server 2014的重点包括内存OLTP.实时的大数据分析.支持混合云端,以及提供更完整的商业智能(B ...

  10. 每日英语:Bosses May Use Social Media to Discriminate Against Job Seekers

    Many companies regularly look up job applicants online as part of the hiring process. A new study su ...