1. 创建一个springboot工程, 在此就不多说了(目录结构).

2. 写一个HTML页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../css/bootstrap.css">
<title>Title</title>
</head>
<body>
<form action="../upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="image/*">
<br>
<input type="submit" value="上传" class="btn btn-success">
</form>
[[${filename}]]
<br>
<img th:src="@{${filename}}" alt="图片">
</body>
</html>

3. 配置application.properties文件, 在配置文件中声明图片的绝对路径及相对路径

server.port=8899
file.upload.path=F://images/
file.upload.path.relative=/images/**

4. 创建一个MyWebAppConfigurer java文件实现WebMvcConfigurer接口, 配置资源映射路径

注: 笔者使用的springboot版本为 2.1.6

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /**
* 资源映射路径
*/
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer { /**上传地址*/
@Value("${file.upload.path}")
private String filePath;
/**显示相对地址*/
@Value("${file.upload.path.relative}")
private String fileRelativePath; @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(fileRelativePath).
addResourceLocations("file:/" + filePath);
}
}

5. 编写Controller层

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; import java.io.File;
import java.io.IOException; @Controller
public class TestController { /**上传地址*/
@Value("${file.upload.path}")
private String filePath; // 跳转上传页面
@RequestMapping("test")
public String test() {
return "Page";
} // 执行上传
@RequestMapping("upload")
public String upload(@RequestParam("file") MultipartFile file, Model model) {
// 获取上传文件名
String filename = file.getOriginalFilename();
// 定义上传文件保存路径
String path = filePath+"rotPhoto/";
// 新建文件
File filepath = new File(path, filename);
// 判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
try {
// 写入文件
file.transferTo(new File(path + File.separator + filename));
} catch (IOException e) {
e.printStackTrace();
}
// 将src路径发送至html页面
model.addAttribute("filename", "/images/rotPhoto/"+filename);
return "Page";
}
}

6. 完成

springboot+thymeleaf 实现图片文件上传及回显的更多相关文章

  1. Ajax图片异步上传并回显

    1.jsp页面 <td width="20%" class="pn-flabel pn-flabel-h"></td> <td w ...

  2. springmvc图片文件上传接口

    springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller; import java.awt.image.Buffer ...

  3. SpringMvc MultipartFile 图片文件上传

    spring-servlet.xml <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id="multipar ...

  4. .Net Core 图片文件上传下载

    当下.Net Core项目可是如雨后春笋一般发展起来,作为.Net大军中的一员,我热忱地拥抱了.Net Core并且积极使用其进行业务的开发,我们先介绍下.Net Core项目下实现文件上传下载接口. ...

  5. SpringBoot系列——附件管理:整合业务表单实现上传、回显、下载

    前言 日常开发中,大多数项目都会涉及到附件上传.回显.下载等功能,本文记录封装通用附件管理模块,并与业务模块进行整合实现上传.回显.下载 我们之前已经对文件上传下载有过记录,传送门:基于"f ...

  6. 图片上传并回显Ajax异步篇

    图片上传并回显Ajax异步篇 图片如何无刷新的上传到服务器呢?继前两篇文章后,我们来实战一下如何无刷新的异步上传图片,我们还是先看一下效果 在实战前呢,我们需要做些准备工作.比如说,了解一下FormD ...

  7. springboot实现简单的文件上传

    承接上一篇,这里记录一下简单的springboot文件上传的方式 首先,springboot简单文件上传不需要添加额外的jar包和配置 这里贴一下后端controller层的实现代码 补一份前台的HT ...

  8. Springboot 一行代码实现文件上传 20个平台!少写代码到极致

    大家好,我是小富~ 技术交流,公众号:程序员小富 又是做好人好事的一天,有个小可爱私下问我有没有好用的springboot文件上传工具,这不巧了嘛,正好我私藏了一个好东西,顺便给小伙伴们也分享一下,d ...

  9. PHP 图片文件上传代码

    通过 PHP,可以把文件上传到服务器.里面加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 为了网站的安全,肯定不让上传php文件,如果有人进入你的后台,上传了一个php文件,你的 ...

随机推荐

  1. 如何禁用Antimalware Service Executable

    有时发现风扇呼呼地转,查看任务管理器,发现其中antimalware service executable占用了大量的CPU和内存. 这是由于Windows Defender软件导致的,可以用如下方法 ...

  2. hdu1495-非常可乐-(倒水问题bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题解: 1.最少次数?江湖套路,bfs.2.怎么倒?从一个杯子倒到另一个杯子.3.倒多少?因为没有刻度,所 ...

  3. Mysql查询数据库状态及信息

    使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小.字符集.用户等等.下面总结了一些查看数据库相关信息的命令 1:查看显示所有数据库 mysql> show databases; ...

  4. CSP2019滚粗记

    CSP2019滚粗记 从不拖更 Day -inf 考完月考才停课\kel,然后月考又被吊打了. Day -1 和左边的\({\text L}\color{red} {\text{ongge}}\)右边 ...

  5. [LeetCode] 35. Search Insert Position 搜索插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. hunter-冲刺合集

    冲刺合集 一·项目相关 作业相关 具体描述 所属班级 2019秋福大软件工程实践Z班 作业要求 团队作业第五次-项目冲刺 作业正文 hunter--冲刺集合 团队名称 hunter小组 作业目标 最终 ...

  7. 使用windows 上的远程连接来远程Linux Ubuntu系统的设置

    实验环境: Windows 10 , VMware Workstation ,Ubuntu16.04 1.root登录ubuntu,然后执行下面的命令 # root账户登录ubuntu ,执行下面的命 ...

  8. [转帖]SQL Server 10分钟理解游标

    SQL Server 10分钟理解游标 https://www.cnblogs.com/VicLiu/p/11671776.html 概述 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合 ...

  9. windows远程桌面无法拷贝文件的问题与解决方法

    在开发完往windows服务器上部署系统或者给系统打补丁的时候,都会需要远程桌面的双向拷贝文件功能. 但是有些时候却会发现没有办法拷贝文件,原因主要有两个. 01 远程桌面的剪贴板设置 一个是在远程桌 ...

  10. Java中Deque特性及API

    美人如斯,文章如斯! 定义 双向队列:支持插入删除元素的线性集合 特性: 插入.删除.获取操作支持两种形式:快速失败和返回null或true/false 既具有FIFO特点又具有LIFO特点,即是队列 ...