SpringBoot2 上传文件 上传多文件
项目结构:
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 上传文件 上传多文件的更多相关文章
- SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...
- SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制
SpringBoot1.x升级SpringBoot2.x踩坑之文件上传大小限制 前言 LZ最近升级SpringBoo框架到2.1.6,踩了一些坑,这里介绍的是文件上传大小限制. 升级前 #文件上传配置 ...
- SpringBoot2.0(三) 文件上传
SpringBoot中发起文件上传示例: /** * 文件上传 * @param multipartFile * @param path * @return */ @RequestMapping(va ...
- SpringBoot2.x设置文件上传文件的大小
The field file exceeds its maximum permitted size of 1048576 bytes spring: # 设置文件上传文件大小 servlet: mul ...
- springboot解决文件上传大小限制
(1)在配置文件(application.properties)加入如下代码 springboot2.0以下配置为: spring.http.multipart.maxFileSize = 10Mb ...
- 使用java的MultipartFile实现layui官网文件上传实现全部示例,java文件上传
layui(谐音:类UI) 是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与组织形式,门槛极低,拿来即用. layui文件上传示例地址:https://www. ...
- SpringBoot整合WEB开发--(三)文件上传
文件上传: Java中文件上传一共涉及到两个组件,CommonsMultipartResolver和StandardServletMultipartResolver,其中CommonsMultipar ...
- 【SpringBoot】07.SpringBoot文件上传
SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...
- 十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑
SpringBoot-基于Yml配置方式,实现文件上传逻辑 1.文件上传 2.搭建文件上传界面 2.1 引入页面模板Jar包 2.2 编写简单的上传页面 2.3 配置页面入口 3.SpringBoot ...
随机推荐
- Vue基本概念介绍及vue-cli环境搭建
1 js中初始化一个Vue对象,传的参数就是对象属性. 挂载点.模板.实例之间的关系. var vm = new Vue({ el:"#app", template:'<di ...
- django性能优化缓存view详解
缓存提升性能: 1.通常的view会去数据库端执行相关的查询然后交由template渲染.数据库访问通常就是性能的瓶颈所在. 2.由于许多数据要很久才会变一次.两次连续的数据库访问通常返回的数据是一样 ...
- FIR基本型仿真_03
作者:桂. 时间:2018-02-05 20:50:54 链接:http://www.cnblogs.com/xingshansi/p/8419452.html 一.仿真思路 设计低通滤波器(5阶,6 ...
- CentOS 6.5 搭建NFS文件服务器
环境介绍:服务器: 192.168.0.1客户机: 192.168.0.2安装软件包:服务器和客户机都要安装nfs 和 rpcbind 软件包:yum -y install nfs-utils rpc ...
- LED音乐频谱之输入数据处理
转载请注明出处:http://blog.csdn.net/ruoyunliufeng/article/details/38023431 通过前面的介绍我们知道.声音信号要通过AD转换,变成我们可以处理 ...
- 【Unity】8.4 扩展UnityGUI
分类:Unity.C#.VS2015 创建日期:2016-04-27 一.简介 有很多种方法可以补充和扩展 UnityGUI 以满足您的需求.你可以混合和创建控件,并且可以有多种方法来规定用户 GUI ...
- 支付宝对账单下载Java沙箱调用
package code; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...
- 沐雪多用户微信公众平台开发源码,商城小程序源码(2018年最新的asp.net C# 微信源码,小程序源码)
现售价5400元,就可以搭建自己的微信平台啦 购买地址:https://item.taobao.com/item.htm?id=539102325336 该系统是由[上海沐雪网络]独家授权销售,其他地 ...
- 【转】(三)unity4.6Ugui中文教程文档-------概要-UGUI Basic Layout
原创至上,移步请戳:(三)unity4.6Ugui中文教程文档-------概要-UGUI Basic Layout 2. BasicLayout 在这一节我们会看到UI元素相对于画布的位置是怎样的. ...
- Python 执行linux 命令
# !/usr/bin/env python # -*- coding: utf-8 -* import os def main(): file_list = os.popen("ls&qu ...