先建工程

只勾选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文件上传的更多相关文章

  1. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  2. 【SpringBoot】07.SpringBoot文件上传

    SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...

  3. SpringBoot 文件上传临时文件路径问题

    年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...

  4. springboot文件上传下载简单使用

    springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...

  5. springboot 文件上传大小配置

    转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...

  6. SpringBoot文件上传下载

    项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...

  7. Springboot 文件上传(带进度条)

    1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

  8. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  9. SpringBoot文件上传异常之提示The temporary upload location xxx is not valid

    原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...

  10. SpringBoot学习6:springboot文件上传

    1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...

随机推荐

  1. git 提交的步骤

    1. git init //初始化仓库   2. git add .(文件name) //添加文件到本地仓库   3. git commit -m "first commit" / ...

  2. Codeforces Round #419 Div. 1

    A:暴力枚举第一列加多少次,显然这样能确定一种方案. #include<iostream> #include<cstdio> #include<cmath> #in ...

  3. AIM Tech Round 4 Div. 1

    A:显然最优方案是对所形成的置换的每个循环排个序. #include<iostream> #include<cstdio> #include<cmath> #inc ...

  4. 51Nod 1381 硬币游戏

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6445369.html 1381 硬币游戏 基准时间限制:1 秒 空间限制:131072 KB 分值 ...

  5. 【XSY2668】排列统计 DP

    题目描述 给你一个长度为\(n\)的排列\(a\),每次要选择两个数,交换这两个数(这两个数可以相同).总共要交换\(k\)次. 最后要统计数列中有多少位置\(i\)满足\(\max_{j\leq i ...

  6. Django media 配置

    Django  media 配置 settings.py 配置  配置 media 的路径, 以及连接到主路径 还要添加一个 上下文管理 TEMPLATES = [ { 'BACKEND': 'dja ...

  7. C#中equal与==的区别

    C#中equal与==的区别 来源 https://www.cnblogs.com/dearbeans/p/5351695.html C#中,判断相等有两种方式,一种是传统的==操作,一种是objec ...

  8. 「CodeForces - 50C 」Happy Farm 5 (几何)

    BUPT 2017 summer training (16) #2B 题意 有一些二维直角坐标系上的整数坐标的点,找出严格包含这些点的只能八个方向走出来步数最少的路径,输出最少步数. 题解 这题要求严 ...

  9. 【模板】cdq分治代替树状数组(单点修改,区间查询)

    #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...

  10. WC2019 划水记

    写在前面: 本篇是擅长咕咕咕的\(\text{BLUESKY007}\)同学难得不咕写的游记,将会记录\(WC2019(2019.1.24(Day\ 0)\sim2019.1.30(Day\ 6))\ ...