先建工程

只勾选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. Prism框架研究(三)

    这一篇主要用来介绍一下基于Prism Library中的核心服务以及如何配置Container,还有一个重要的部分是如何管理各个组件之间的依赖性,下面就这些内容来做一一的介绍. 1 Prism中的核心 ...

  2. jdbc 接口的用法 Statement和PreparedStatement的区别!

    package cn.zhouzhou; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Resu ...

  3. 一、IntelliJ IDEA创建java项目

    一.IntelliJ IDEA创建java项目 二.IntelliJ IDEA下载并包含jdbc包 1.下载zip格式的驱动包:https://dev.mysql.com/downloads/conn ...

  4. js的常用文档对象,document

    1.document的概念:window的子对象,由于DOM对象模型的默认对象就是window,因此Window对象中的方法和子对象不需要通过Window来引用. - 2.document的组成:属性 ...

  5. Deconvolution用法

  6. React 学习(一) ---- React Element /组件/JSX

    学习React的时候,你可能听到最多的就是要先学习webpack, babel,要先学会配置然后才能学react 等等,一堆的配置就把我们吓着了,根本就没有心情就学习react了.其实在最开始学习re ...

  7. Linux下git的使用——将已有项目放到github上

    本地已经有一个项目了,需要将该项目放到github上,怎么操作? 步骤: 本地安装git,有github账号是前提. (1)先在github创建一个空的仓库,并复制链接地址.使用https,以.git ...

  8. mvc HTML转Excel身份证后三位变成0

    @{ var style = "vnd.ms-excel.numberformat:@"; } //HTML <td style=@style><span> ...

  9. oracle总结--增删改查

    oracle的执行计划SQL> EXPLAIN PLAN FOR SELECT * FROM emp;已解释.SQL> SELECT plan_table_output FROM TABL ...

  10. vscode——配置git的path

    设置 打开设置 搜索配置 复制Json文本 编辑配置 粘贴刚才复制的json文本,并将自己git的地址写好,保存即可 完整配置 { "workbench.colorTheme": ...