springboot项目中freemarker与jsp如何添加文件到数据库
时间: 2023-12-16 16:03:36 浏览: 33
在 Spring Boot 项目中,您可以借助 Spring 的集成来更方便地使用 FreeMarker 和 JSP,同时使用 Spring 的 JDBC 模块来与数据库进行交互。以下是一个示例代码,演示如何将文件存储到 MySQL 数据库中:
1. 添加依赖
在 pom.xml 文件中添加以下依赖:
```xml
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- FreeMarker 模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- JDBC 模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Tomcat JDBC 连接池 -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
</dependencies>
```
2. 配置数据库连接
在 application.properties 文件中添加数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=username
spring.datasource.password=password
```
3. 定义文件实体类
创建一个名为 FileEntity 的类,用于表示文件实体:
```java
public class FileEntity {
private Long id;
private String name;
private byte[] content;
// 省略 getter 和 setter 方法
}
```
4. 定义文件存储服务
创建一个名为 FileStorageService 的接口,定义文件存储服务的方法:
```java
public interface FileStorageService {
void save(FileEntity file);
FileEntity getById(Long id);
}
```
在实现类中,使用 Spring 的 JdbcTemplate 对象来执行 SQL 语句:
```java
@Service
public class FileStorageServiceImpl implements FileStorageService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void save(FileEntity file) {
String sql = "INSERT INTO files (name, content) VALUES (?, ?)";
jdbcTemplate.update(sql, file.getName(), file.getContent());
}
public FileEntity getById(Long id) {
String sql = "SELECT * FROM files WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new RowMapper<FileEntity>() {
public FileEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
FileEntity file = new FileEntity();
file.setId(rs.getLong("id"));
file.setName(rs.getString("name"));
file.setContent(rs.getBytes("content"));
return file;
}
});
}
}
```
5. 定义控制器
创建一个名为 FileController 的控制器,在其中注入 FileStorageService,并提供上传文件和下载文件的方法:
```java
@Controller
public class FileController {
@Autowired
private FileStorageService fileStorageService;
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
if (!file.isEmpty()) {
FileEntity fileEntity = new FileEntity();
fileEntity.setName(file.getOriginalFilename());
fileEntity.setContent(file.getBytes());
fileStorageService.save(fileEntity);
}
return "redirect:/";
}
@RequestMapping(value = "/download/{id}", method = RequestMethod.GET)
public void download(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
FileEntity fileEntity = fileStorageService.getById(id);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileEntity.getName() + "\"");
response.getOutputStream().write(fileEntity.getContent());
}
}
```
6. 编写 FreeMarker 或 JSP 视图
在 FreeMarker 或 JSP 视图中,您可以使用类似以下的语句来上传文件:
```html
<form method="post" enctype="multipart/form-data" action="/upload">
<input type="file" name="file"/>
<button type="submit">上传</button>
</form>
```
使用类似以下的语句来下载文件:
```html
<a href="/download/${file.id}">${file.name}</a>
```
在上述代码中,${file.id} 和 ${file.name} 是从数据库中检索文件得到的。