SpringBoot文件上传
先建工程



只勾选web和freemarker模板

最后

先看一下最终目录结构

先修改pom文件,加入common-io依赖

然后修改Application.yml文件
spring:
freemarker:
templateLoaderPath: classpath:/templates/
content-type: text/html
charset: UTF-8
suffix: .ftl
然后新建一个controller
package com.example.demo.controller; import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException; @RestController
public class FileController { @RequestMapping("/index")
public ModelAndView index(){
ModelAndView mv = new ModelAndView("index");
return mv;
} @RequestMapping("/upload")
public String upload(HttpServletRequest request, @RequestParam("myFile")MultipartFile file) throws IOException {
if (file.isEmpty()) return "No File"; String name = file.getOriginalFilename();
System.out.println("FileName:" + name); String fileType = name.substring(name.lastIndexOf("."));
System.out.println("FileType:" + fileType); String size = FileUtils.byteCountToDisplaySize(file.getSize());
System.out.println("FileSize:" + size); String path = request.getSession().getServletContext().getRealPath("/static/img/");
File desFile = new File(path+name);
FileUtils.copyInputStreamToFile(file.getInputStream(), desFile); String info = desFile.getAbsolutePath(); return info;
} }
最后新建一个页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Upload">
</form>
</body>
</html>
PS:这里没有启动类的事,因为启动类的上层没有任何代码(官方建议启动类放在根目录)
启动应用

..

..返回结果

..查看控制台

..找到本地文件

..看看可不可以访问

SpringBoot文件上传的更多相关文章
- 补习系列(11)-springboot 文件上传原理
目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...
- 【SpringBoot】07.SpringBoot文件上传
SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...
- SpringBoot 文件上传临时文件路径问题
年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...
- springboot文件上传下载简单使用
springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- SpringBoot文件上传下载
项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...
- Springboot 文件上传(带进度条)
1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- SpringBoot文件上传异常之提示The temporary upload location xxx is not valid
原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...
- SpringBoot学习6:springboot文件上传
1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...
随机推荐
- linux中一些特殊的中文文件不能删除问题
例: [root@iZ2zecl4i8oy1rvs00dqzeZ tmp]# ,),(,,' [root@iZ2zecl4i8oy1rvs00dqzeZ tmp]# echo "rm -rf ...
- Mvc校验用户没有登录就跳转的实现
看字面意思很简单,就是判断用户是否登录了,如果没有登录就跳转到登陆页面. 没错,主要代码如下(这里就不写判断登录了,直接跳转) 首先在控制器中新建一个BaseController public cla ...
- LODOP.FORMAT格式转换【回调和直接返回值】
Lodop中有一些格式转换函数,这些函数和其他众多函数一样,c-lodop需要使用回调函数On_Return返回,Lodop插件方式直接返回,通常混合部署,写法要兼容两个控件.可以用if (LODOP ...
- Python介绍及环境配置
Python 简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...
- python史上最全学习路线图
ps:盘它 python入门教程 关注微信公众号,回复"python入门"获取视频下载地址
- HTML条件注释
前面的话 IE条件注释是微软从IE5开始就提供的一种非标准逻辑语句,作用是可以灵活的为不同IE版本浏览器导入不同html元素.很显然这种方法的最大好处就在于属于微软官方给出的兼容解决办法而且还能通过W ...
- BZOJ3876[Ahoi2014&Jsoi2014]支线剧情——有上下界的最小费用最大流
题目描述 [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往 都有很多的支线剧情,现在JYY想花费 ...
- BZOJ1565[NOI2009]植物大战僵尸——最大权闭合子图+拓扑排序
题目描述 Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻.该款游戏包含多 ...
- Java json转model
前面有一篇关于 json的转换类的工具:http://blog.csdn.net/hanjun0612/article/details/77891569 但是有一个情况. 由于java需要属性小写开 ...
- Breakable loop in Scratch
Breakable loop in Scratch https://stackoverflow.com/questions/30682144/breakable-loop-in-scratch Bre ...