thymeleaf中 读数据库br

thymeleaf中 读数据库</br>
主要是标签th:text 换成 th:utext
这样页面就不会显示<br>
而是直接换行

好的,下面是一个简单的Spring Boot接口,用于上传图片并将图片路径保存到数据库。 1. 首先,我们需要在pom.xml添加以下依赖,以支持文件上传: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> ``` 2. 然后,我们需要创建一个实体类,用于保存上传的图片信息。代码如下: ```java import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Image { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String path; // getters and setters } ``` 3. 接着,我们需要创建一个数据访问接口,用于对图片实体进行CRUD操作。代码如下: ```java import org.springframework.data.jpa.repository.JpaRepository; public interface ImageRepository extends JpaRepository<Image, Long> { } ``` 4. 然后,我们需要创建一个文件上传控制器,用于处理上传文件的HTTP请求。代码如下: ```java import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.UUID; @Controller @RequestMapping("/image") public class ImageController { @Autowired private ImageRepository imageRepository; @GetMapping("/{id}") public void getImage(@PathVariable Long id, HttpServletResponse response) throws IOException { Image image = imageRepository.findById(id).orElse(null); if (image != null) { response.setContentType("image/jpeg"); InputStream inputStream = new FileInputStream(new File(image.getPath())); IOUtils.copy(inputStream, response.getOutputStream()); } } @PostMapping("/upload") @ResponseBody public String uploadImage(@RequestParam("file") MultipartFile file) throws IOException { if (!file.isEmpty()) { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); if (fileExt.equals("jpg") || fileExt.equals("jpeg") || fileExt.equals("png")) { String newFileName = UUID.randomUUID().toString() + "." + fileExt; String filePath = "D:/images/" + newFileName; File dest = new File(filePath); file.transferTo(dest); Image image = new Image(); image.setName(fileName); image.setPath(filePath); imageRepository.save(image); return "/image/" + image.getId(); } } return null; } } ``` 5. 最后,我们需要创建一个Thymeleaf模板,用于演示如何上传图片。代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>上传图片</title> </head> <body> <h1>上传图片</h1> <form method="post" enctype="multipart/form-data" action="/image/upload"> <input type="file" name="file"/><br/> <input type="submit" value="上传"/> </form> </body> </html> ``` 现在,我们已经完成了一个简单的图片上传接口,可以将上传的图片保存到指定目录,并将图片路径保存到数据库。可以通过访问"/image/upload"来上传图片。上传成功后,接口会返回图片访问路径,例如"/image/1"。可以通过访问该路径来查看上传的图片。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值