170706、springboot编程之文件上传
使用thymleaf模板,自行导入依赖!
一、单文件上传
1、编写单文件上传页面singleFile.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>单文件上传</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/singleUpload">
<p>选择文件:<input type="file" name="file"/></p>
<p><input type="submit" th:value="上传"/></p>
</form>
</body>
</html>
2、编写FileUploadController.java
package com.rick.apps.controller; import com.rick.common.ResultJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream; /**
* Desc : 文件上传
* User : RICK
* Time : 2017/8/23 9:36
*/ @Controller
public class FileUploadController { /**
* Desc : 跳转单文件上传页面
* User : RICK
* Time : 2017/8/23 9:37
*/ @RequestMapping("/singleFile")
public String singleFile(){
System.out.println("-------------------");
return"/singleFile";
} /**
* Desc : 单文件上传
* 注意:不指定上传目录,默认是上传到项目的根目录
* User : RICK
* Time : 2017/8/23 9:40
*/
@ResponseBody
@PostMapping("/singleUpload")
public ResultJson singleUpload(@RequestParam("file")MultipartFile file){
if (!file.isEmpty()){
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
out.write(file.getBytes());
out.flush();
out.close();
} catch(Exception e){
e.printStackTrace();
return ResultJson.buildFailInstance("上传失败");
}
} else {
return ResultJson.buildFailInstance("上传失败,文件为空!");
}
return ResultJson.buildSuccessInstance();
}
}
3、编写文件上传的设置
package com.rick; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean; import javax.servlet.MultipartConfigElement; @SpringBootApplication
@EnableConfigurationProperties
@ServletComponentScan
public class SpringbootEdu01Application { public static void main(String[] args) {
SpringApplication.run(SpringbootEdu01Application.class, args);
} /**
* Desc : 设置文件上传的基本配置
* User : RICK
* Time : 2017/8/23 10:11
*/ @Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
//设置文件大小限制 ,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了;
factory.setMaxFileSize("1MB");//KB,MB
//设置总上传数据总大小
factory.setMaxRequestSize("10MB");////KB,MB
//设置文件存放位置
// factory.setLocation("d:\\files");
return factory.createMultipartConfig();
}
}
4、启动项目测试
访问http://localhost:8080/singleFile出现文件上传页面

选择要上传的文件,点击上传


上传成功,到项目根目录下查看文件是否存在

项目清单:

二、多文件上传
1、编写多文件上传页面multFile.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>多文件上传</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/multUpload">
<p>文件1:<input type="file" name="file" /></p>
<p>文件2:<input type="file" name="file" /></p>
<p>文件3:<input type="file" name="file" /></p>
<p><input type="submit" value="上传" /></p>
</form>
</body>
</html>
2、编写多文件上传后台代码FileUploadController.java
package com.rick.apps.controller; import com.rick.common.ResultJson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List; /**
* Desc : 文件上传
* User : RICK
* Time : 2017/8/23 9:36
*/ @Controller
public class FileUploadController { /**
* Desc : 跳转单文件上传页面
* User : RICK
* Time : 2017/8/23 9:37
*/
@RequestMapping("/singleFile")
public String singleFile(){
return"/singleFile";
} /**
* Desc : 单文件上传
* 注意:不指定上传目录,默认是上传到项目的根目录
* User : RICK
* Time : 2017/8/23 9:40
*/
@ResponseBody
@PostMapping("/singleUpload")
public ResultJson singleUpload(@RequestParam("file")MultipartFile file){
if (!file.isEmpty()){
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
out.write(file.getBytes());
out.flush();
out.close();
} catch(Exception e){
e.printStackTrace();
return ResultJson.buildFailInstance("上传失败");
}
} else {
return ResultJson.buildFailInstance("上传失败,文件为空!");
}
return ResultJson.buildSuccessInstance();
} /**
* Desc : 跳转多文件上传页面
* User : RICK
* Time : 2017/8/23 9:37
*/
@RequestMapping("/multFile")
public String multFile(){
return"/multFile";
} /**
* Desc : 多文件上传
* 主要是使用了MultipartHttpServletRequest和MultipartFile
* User : RICK
* Time : 2017/8/23 10:17
*/
@ResponseBody
@PostMapping("/multUpload")
public ResultJson multUpload(HttpServletRequest request){
try {
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i=0;i<files.size();i++){
file = files.get(i);
if(!file.isEmpty()){
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
stream.write(bytes);
stream.flush();
stream.close();
}
}
} catch(Exception e){
e.printStackTrace();
return ResultJson.buildFailInstance("上传失败");
}
return ResultJson.buildSuccessInstance();
} }
3、启动项目测试,http://localhost:8080/multFile

选择文件

点击上传

到项目根目录下查看文件是否上传成功

项目清单:

170706、springboot编程之文件上传的更多相关文章
- Windows环境下用C#编程将文件上传至阿里云OSS笔记
Windows环境下用C#编程将文件上传至阿里云OSS笔记 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/ex_net/article/detai ...
- SpringBoot项目实现文件上传和邮件发送
前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...
- Springboot如何启用文件上传功能
网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文 ...
- iOS-网络编程(二)文件上传和断点离线下载
一. iOS中发送HTTP请求的方案 在iOS中,我们常用发送HTTP请求的方案有苹果原生(自带)NSURLConnection:用法简单,最古老最经典最直接的一种方案 (iOS 9.0弃用)NSUR ...
- SpringBoot+BootStrap多文件上传到本地
1.application.yml文件配置 # 文件大小 MB必须大写 # maxFileSize 是单个文件大小 # maxRequestSize是设置总上传的数据大小 spring: servle ...
- SpringBoot之KindEditor文件上传
后端核心代码如下: package com.blog.springboot.controller; import java.io.BufferedOutputStream; import java.i ...
- springboot+vue实现文件上传
https://blog.csdn.net/mqingo/article/details/84869841 技术: 后端:springboot 前端框架:vue 数据库:mysql pom.xml: ...
- SpringBoot: 6.文件上传(转)
1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- Springboot(九).多文件上传下载文件(并将url存入数据库表中)
一. 文件上传 这里我们使用request.getSession().getServletContext().getRealPath("/static")的方式来设置文件的存储 ...
随机推荐
- Linux ad7606 驱动
Linux中已经移植好了ad7606,位于driver/staging/iio/adc/目录中.只要在板级文件中添加device中即可. 移植参考文档: https://wiki.analog.com ...
- Spring IO platform 简介
前提:熟悉Spring基础知识. 简介:Spring IO Platform将 the core Spring APIs 集成到一个Platform中.它提供了Spring portfolio中的大量 ...
- 第三百零七节,Django框架,models.py模块,数据库操作——表类容的增删改查
Django框架,models.py模块,数据库操作——表类容的增删改查 增加数据 create()方法,增加数据 save()方法,写入数据 第一种方式 表类名称(字段=值) 需要save()方法, ...
- pip 安装库过慢
对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题. 所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装 ...
- Hello World! 这是我的第一个 CGI 程序
Hello World! 这是我的第一个 CGI 程序上面的 C++ 程序是一个简单的程序,把它的输出写在 STDOUT 文件上,即显示在屏幕上.在这里,值得注意一点,第一行输出 Content-ty ...
- (转)引用---FFMPEG解码过程
视频播放过程 首先简单介绍以下视频文件的相关知识.我们平时看到的视频文件有许多格式,比如 avi, mkv, rmvb, mov, mp4等等,这些被称为容器(Container), 不同的容器格式规 ...
- bootstrap -- css -- 按钮
本文中提到的按钮样式,适用于:<a>, <button>, 或 <input> 元素上 但最好在 <button> 元素上使用按钮 class,避免跨浏 ...
- 使用什么工具连接MySQL Server
字符界面:命令行终端(需MySQL Client) GUI界面:Navicat.MySQL Workbench 开发语言:使用相应语言的MySQL数据库驱动包或模块连接MySQL 我一般用的是命令行, ...
- shiro缓存
shiro的可以权限控制内容包括:URL地址.Web页面的元素.以及方法,即shiro对用户权限的控制是细粒度的.从用户的一次访问来说,他可能需要最多经过三种.多次的验证.这里的多次怎么说呢?如果说W ...
- fiddler抓包,搞定接口
上篇介绍的世纪佳缘登录是由已有cookie保持登录状态的.世纪佳缘登陆不需要填入验证码,可以很方便直接请求登录接口来达到登录状态的目的. 这篇介绍直接从登录接口进行登录,那么这就要求要找到登录接口ur ...