项目结构:

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. Java代码通过API操作HBase的最佳实践

    HBase提供了丰富的API.这使得用Java连接HBase非常方便. 有时候大家会使用HTable table=new HTable(config,tablename);的方式来实例化一个HTabl ...

  2. 基础005_V7-Select IO

    主要参考ug471.pdf.

  3. curl以cookie的方式登录

    curl -o /dev/null -s -w ‘%{time_connect}:%{time_starttransfer}:%{time_total}’ --cookie "UM_dist ...

  4. logtash 接收多配置文件

    [root@10-19-148-59 etc]# vim front_esb.conf input { beats { type => beats port => 5077 } } fil ...

  5. 分享十:php中并发读写文件冲突的解决方案

    对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果并发高,在我们对文件进行读写操作时,很有可能多个进程对进一文件进行操作,如果这时不对文件的访问进行相 ...

  6. Spring 注解@Component,@Service,@Controller,@Repository

    Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...

  7. C++中的typedef typename 作用

    今天在代码里看到了这样一段代码: typedef typename RefBase::weakref_type weakref_type; 起初一直搞不懂为什么要加个typename,后来搜索了一下才 ...

  8. Fluent UDF【3】:环境配置

    windows操作系统下UDF的编译需要借助Visual Studio中的C编译器.因此若要想编译UDF,则必须事先配置好编译环境. Visual Studio Visual Stuido(后面简称V ...

  9. linux安全配置检查脚本_v0.5

    看到网上有人分享了一些linux系统的基线检查脚本,但有些检查项未必适合自己或者说检查的不够完善, 计划按着自己的需求重新写一份出来,其中脚本的检查范围在不断更新中. 脚本内容: [root@loca ...

  10. (原创)拨开迷雾见月明-剖析asio中的proactor模式(二)

    在上一篇博文中我们提到异步请求是从上层开始,一层一层转发到最下面的服务层的对象win_iocp_socket_service,由它将请求转发到操作系统(调用windows api),操作系统处理完异步 ...