项目结构:

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. linux(centos6)搭建ftp服务器 -摘自网络

    前提 ssh服务已经开启,关闭防火墙,主机和虚拟机能ping通 查看ssh和防火墙的状态 service sshd status service iptables status 开启ssh服务 ser ...

  2. 设计模式-装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活

  3. scikit-learn:6. Strategies to scale computationally: bigger data

    參考:http://scikit-learn.org/stable/modules/scaling_strategies.html 对于examples.features(或者两者)数量非常大的情况, ...

  4. FFmpeg(5)-AVStream和AVCodecParameters部分参数分析

    一.AVStream AVCodecContext *codec // 已过时,使用另一个 codecpar 结构体代替. AVRational time_base // 时间基数. int64_t ...

  5. Google MapReduce到底解决什么问题?

    很多时候,定义清楚问题比解决问题更难. 什么是MapReduce? 它不是一个产品,而是一种解决问题的思路,它有多个工程实现,Google在论文中也给出了它自己的工程架构实现. MapReduce这个 ...

  6. 每日英语:China to Move Slowly on One-Child Law Reform

    BEIJING—China's family-planning agency is projecting a slow rollout for an easing of its one-child p ...

  7. DIOCP开源项目-DIOCP3的LoadRunner11测试报告

    昨天有个多年的群友(B3.Locet)用LoadRunner11对DIOCP3做压力测试,说测试的时候出现了大量的10053,10054的报告.昨天晚上下载了个LoadRunner11, 今天捣鼓了下 ...

  8. 玩转Bootstrap(JS插件篇)-第1章 模态弹出框 :1-4 模态弹出框--结构分析

    模态弹出框--结构分析 Bootstrap框架中的模态弹出框,分别运用了“modal”.“modal-dialog”和“modal-content”样式,而弹出窗真正的内容都放置在“modal-con ...

  9. Linux内核分析:recv、recvfrom、recvmsg函数实现

    先看一下这三个函数的声明: #include <sys/types.h> #include <sys/socket.h> ssize_t recv(int sockfd, vo ...

  10. 2015-2016款Mac安装win10多分区教程,不破坏GUID分区表。

    原文:https://bbs.feng.com/read-htm-tid-10895240.html 参考:https://bbs.feng.com/read-htm-tid-9940193.html ...