当前位置:网站首页>Thymeleaf uploads files in web pages
Thymeleaf uploads files in web pages
2022-07-21 07:18:00 【Cheng Cheng, it's Xiaobai】
Catalog
This is thymeleaf stay pom Dependency added in ( You can't be fat at one go )
Use thymeleaf Upload ( A photo )
First write and configure the virtual machine
To write controller Layer code
stay resource Of templates Add... To the file
The code block is as follows :
stay application.properties The contents of the document
The code block is as follows :
This article is my previous notes , If I saw it on the Nuggets, it's right , Or myself Cheng Cheng My personal homepage - dynamic - Nuggets This is the only one . If there is anything wrong, you are welcome to point out the problem , I am a little white .
The link of the last article is :MySQL Note down _ Cheng Cheng is Xiaobai's blog -CSDN Blog , This article is a connection Automatic configuration and thymeleaf template engine _ Cheng Cheng is Xiaobai's blog -CSDN Blog .
If it is springboot If so, please see springboot Create project _ Cheng Cheng is Xiaobai's blog -CSDN Blog , This is from scratch, not finished springboot, Study together !!!!
This is thymeleaf stay pom Dependency added in ( You can't be fat at one go )

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Use thymeleaf Upload ( A photo )
First write and configure the virtual machine

The code block is as follows
@Configuration
public class TextThymeleaf implements WebMvcConfigurer {
// Configure virtual machines
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
//upload/** The path of opening up
//addResourceHandlers() Configure resource paths , Point to the real path where the resource is located
registry.addResourceHandler("/upload/**").addResourceLocations("file:E:\\wy/");
}
}
To write controller Layer code

The code block is as follows
@Controller
public class TextController {
@RequestMapping("/fileLoad")
public String uploadController(@RequestParam("photo") MultipartFile file, Model model){
if (file.isEmpty()){
model.addAttribute("msg"," Upload file error !!!");
}else {
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
String newFileName= sdf.format(new Date())+file.getOriginalFilename();
Path path= Paths.get("e://wy/"+newFileName);
try {
Files.write(path,file.getBytes());
model.addAttribute("msg"," File upload succeeded ");
model.addAttribute("src",newFileName);
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("msg"," File upload failed ");
}
}
return "status";
}
}
stay resource Of templates Add... To the file

The content of the first file

The code block is as follows
<!DOCTYPE html>
<html lang="en" xmlns:th="http://wwww.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${msg}"></span>
<div th:if="${msg == ' Cheng Cheng's file was uploaded successfully '}">
<img th:src="|upload/${src}|">
</div>
</body>
</html>
Second document

The code block is as follows :
<!DOCTYPE html>
<html lang="en" xmlns:th="http://wwww.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/fileLoad" method="post" enctype="multipart/form-data">
<input type="file" name="photo"><br/>
<input type="submit" value=" Submit ">
</form>
</body>
</html>
stay application.properties The contents of the document

The code block is as follows :
server.port=8082
# Turn off page caching
spring.thymeleaf.cache=false
# Set the default access path
spring.web.resources.static-locations=classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/



This article is my previous notes , If I saw it on the Nuggets, it's right , Or myself Cheng Cheng My personal homepage - dynamic - Nuggets This is the only one . If there is anything wrong, you are welcome to point out the problem , I am a little white .
The link of the last article is :MySQL Note down _ Cheng Cheng is Xiaobai's blog -CSDN Blog , This article is a connection Automatic configuration and thymeleaf template engine _ Cheng Cheng is Xiaobai's blog -CSDN Blog .
If it is springboot If so, please see springboot Create project _ Cheng Cheng is Xiaobai's blog -CSDN Blog , This is from scratch, not finished springboot, Study together !!!!
边栏推荐
- 霸榜MS MARCO!基于Transformer的混合列表感知排序模型
- 【Flutter -- 顶部导航栏】TabBarView 的基本使用
- jsonp解决跨域插件(js、ts)
- (note) feature fusion method in neural network (add/connect)
- Rush's fluid usage (fltk UI Designer)
- Deeply understand MySQL transaction isolation level and locking mechanism
- CV (3)- Loss Functions and Optimization
- uni-app开发之HBuilderX连接夜神模拟器
- CV (2)- image classification
- [computer gadget recommendation] sedentary reminder
猜你喜欢
ECCV 2022 | Kuangshi proposed a semi supervised target detection model, dense teacher, which achieved SOTA performance
Data quality construction of data warehouse
JVM 基础
【Flutter -- 基础组件】AppBar 的使用
[shutter -- basic component] use of AppBar
Possibilities and limitations of defi in the metauniverse
外设篇:I2C通信详解
加密市场中的14个心智模型
Rush's fluid usage (fltk UI Designer)
动态内存开辟
随机推荐
What is PostgreSQL? The relational database that developers love and want most on stackoverflow
Data quality construction of data warehouse
C陷阱与缺陷 第1章 词法“陷阱” 1.5 字符与字符串
Dynamic memory management (malloc, free, calloc, realloc, flexible array)
Eat CPU's OpenMP program "suggestions collection"
Pyspark: conversion operation and action operation of dataframe
解析「Web3悖论」的内在机理与突破路径
Convert datasets to VOC format
Moco V2: further upgrade of Moco series
ACTF出题(dropper+master_of_dns)
《运营商数据存力指标》发布,透露了哪些重要趋势?
哲学的起源和发展-黑格尔-外在历史形态
What can testers do when there is an online bug?
Golang-- operation date
谈谈算法岗简历优化与面试技巧!
[C language] bubble sort upgraded version --- applicable to any type
Possibilities and limitations of defi in the metauniverse
Few-shot Video-to-Video(NeurIPS 2019)视频生成论文解读
KubeSphere 3.3.0 离线安装教程
生态系统长期观测数据产品体系