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")的方式来设置文件的存储 ...
随机推荐
- 关于Cocos2d-x中根据分数增加游戏难度的方法
1.GameScene.h中声明一些分数边界值 //level提升所需的分数 enum LevelUp_Score { Level1Up_Score = , Level2Up_Score = , Le ...
- Ubuntu系统启动报错:The system is running in low-graphics mode
最近,不小心将自己的Ubuntu-12.04桌面系统搞坏了,主要是由于改变了/var目录下文件的属主,结果桌面系统崩溃了,启动都成问题了.不过还算幸运,可以通过其他的机器登录到我的系统上.根据别人的系 ...
- CentOS 7在桌面添加快捷方式
直接把 /usr/share/applications 对应的 xxx.desktop 文件复制到桌面就OK!比如要在桌面创建Google Chrome Browser的快捷方式,直接在找到 /usr ...
- 【复杂】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...
- [mysql] 查询前几条记录
From: http://www.cnblogs.com/xuxm2007/archive/2010/11/16/1878211.html SELECT * FROM table LI ...
- POJ 3211 Washing Clothes 背包题解
本题是背包问题,可是须要转化成背包的. 由于是两个人洗衣服,那么就是说一个人仅仅须要洗一半就能够了,由于不能两个人同一时候洗一件衣服,所以就成了01背包问题了. 思路: 1 计算洗完同一颜色的衣服须要 ...
- link with editor
在左侧explore上,有个双向的箭头,点一下,就会把路径和当前文件自动对应
- Unity 移动端的复制这么写
游戏上线很久了,有些玩家慢慢就流失了,为了让刚流失的玩家再度回归所以做了召回功能!如果一个200级的玩家10天没上线且成功召回的,就会给予召回玩家丰厚的奖励! Q:那如何召回这个流失的玩家呢? A:召 ...
- dedecms的arclist循环中判断第一个li添加css,否则不加
dedecms的arclist循环中,判断如果是第一个li,则添加固定的css,否则不加 写法如下: {dede:arclist row=4 flag='p'} <li [field:glo ...
- PHP实现一个ip(如:127.0.0.1)和多个域名(虚拟主机)的绑定
解决方案一:通过端口来区分不同的虚拟主机 ①按照绑定一个站点的方法做好准备 1. 先开发好自己的网站(d:/myblog(存放在D盘的myblog目录下)) 2. 配置httpd.conf文件(存放在 ...