使用百里香叶的春季启动上传和下载示例。在本文中,我们将学习如何从数据库上传和下载文件。
上传和下载文件是任何应用程序的重要组成部分之一。众所周知,我们使用 Spring Boot 使开发过程变得简单。因此,在这里我们将创建一个示例来从数据库上传和下载文件。我们将在视图层使用百里香叶模板。Spring Data JPA 将在数据访问层使用。这里的关系数据库是MYSQL。
在此示例中,我们将创建一个视图,在其中查看如何上传和下载文件。
第 1 步:打开 IDE STS-弹簧工具套件
第 2 步:转到 Spring 入门项目>文件。
步骤3: 现在,填写如下所示的所有字段,然后单击下一步。
步骤4: 现在,添加百里香叶,春季数据JPA,龙目岛和春季网络的依赖项,然后单击下一步>完成。
现在,等待一段时间,您的项目结构将准备就绪。转到pom.xml文件,您将看到将自动添加以下依赖项
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.6
upload.download
springboot-thymeleaf-mysql-upload-download-files
1.0-SNAPSHOT
jar
springboot-thymeleaf-mysql-upload-download-files
http://localhost:8080
UTF-8
1.8
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
javax.xml.bind
jaxb-api
2.4.0-b180830.0359
org.springframework.boot
spring-boot-maven-plugin
mysql> create database updownload;
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/updownload?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#Multipart
spring.servlet.multipart.enabled=true
spring.servlet.multipart.file-size-threshold=2KB
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=215MB
FileEntity .java
package upload.download.entity;
import javax.persistence.*;
@Entity
@Table(name = "file_table")
public class FileEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "file_id")
private Long fileId;
@Column(name = "file_name")
private String fileName;
@Column(name = "file_type")
private String fileType;
@Lob
@Column(name = "file_byte")
private byte[] fileByte;
public FileEntity() {
}
public FileEntity(String fileName, String fileType, byte[] fileByte) {
this.fileName = fileName;
this.fileType = fileType;
this.fileByte = fileByte;
}
public Long getId() {
return fileId;
}
public void setId(Long id) {
this.fileId = id;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public byte[] getFileByte() {
return fileByte;
}
public void setFileByte(byte[] fileByte) {
this.fileByte = fileByte;
}
}
FileInfo.java
package upload.download.entity;
public class FileInfo {
private String fileInfoName;
private String fileURL;
public FileInfo() {
}
public FileInfo(String fileInfoName, String fileURL) {
this.fileInfoName = fileInfoName;
this.fileURL = fileURL;
}
public String getFileInfoName() {
return fileInfoName;
}
public void setFileInfoName(String fileInfoName) {
this.fileInfoName = fileInfoName;
}
public String getFileURL() {
return fileURL;
}
public void setFileURL(String fileURL) {
this.fileURL = fileURL;
}
}
在数据库中,它将像这样显示
现在,使用 MYSQL 工作台将 TinyBLOB 数据类型更改为LongBlob。
这里的存储库是 DAO 层,它执行所有数据库操作。创建 FileEntityRepository 接口,该接口将扩展 JPARepository
package upload.download.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;
import upload.download.entity.FileEntity;
@Repository
@EnableJpaRepositories
public interface FileEntityRepository extends JpaRepository {
FileEntity findByFileName(String fileName);
}
对网页的请求将由控制器类中的处理程序方法使用 @GetMapping 处理。
package upload.download.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import upload.download.entity.FileEntity;
import upload.download.repository.FileEntityRepository;
import java.util.ArrayList;
import java.util.List;
@Controller
public class UploadController {
@Autowired
private FileEntityRepository fileEntityRepository;
/**
* Get index page
*
* @return index
*/
@GetMapping("/")
public String getIndexFilePage() {
return "index";
}
@PostMapping("/")
public String uploadFiles(@RequestParam("files") MultipartFile[] multipartFiles, Model model) {
List listFileNames = new ArrayList<>();
try {
List storeFiles = new ArrayList<>();
for (MultipartFile file : multipartFiles) {
FileEntity fileEntity = fileEntityRepository.findByFileName(file.getOriginalFilename());
if (fileEntity != null) {
fileEntity.setFileByte(file.getBytes());
} else {
fileEntity = new FileEntity(file.getOriginalFilename(), file.getContentType(), file.getBytes());
}
listFileNames.add(file.getOriginalFilename());
storeFiles.add(fileEntity);
}
// save all files
fileEntityRepository.saveAll(storeFiles);
// successfully message after uploaded
model.addAttribute("message", "Files uploaded successfully!");
model.addAttribute("files", listFileNames);
} catch (Exception e) {
// fail message for unsupported file or max size
model.addAttribute("message", "Fail");
model.addAttribute("files", listFileNames);
}
return "index";
}
}
package upload.download.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import upload.download.entity.FileEntity;
import upload.download.entity.FileInfo;
import upload.download.repository.FileEntityRepository;
import java.util.List;
import java.util.stream.Collectors;
@Controller
public class DownloadController {
@Autowired
private FileEntityRepository fileEntityRepository;
/**
* Get and display file stored in the database.
*
* @return download-file page
*/
@GetMapping("/download/files")
public String getListOfFiles(Model model) {
List fileInfoList = fileEntityRepository.findAll()
.stream()
.map(fileEntity -> {
String fileInfoName = fileEntity.getFileName();
String fileURL = MvcUriComponentsBuilder
.fromMethodName(
DownloadController.class,
"downloadFile",
fileEntity.getFileName())
.build()
.toString();
return new FileInfo(fileInfoName, fileURL);
}
).collect(Collectors.toList());
model.addAttribute("files", fileInfoList);
return "download-file";
}
/**
* Get file to download.
*
* @return file from database to be downloaded
*/
@GetMapping("/download/files/{fileInfoName}")
public ResponseEntity downloadFile(@PathVariable String fileInfoName) {
FileEntity downloadFileEntity = fileEntityRepository.findByFileName(fileInfoName);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + downloadFileEntity.getFileName() + "\"")
.body(downloadFileEntity.getFileByte());
}
}
@Controller注释将学生控制器类标记为请求处理程序。现在让我们打破上面的代码并理解它。
转到 src/main/resources/template 文件夹并创建一个 index.html 文件。现在在寄存器中.html文件确保添加以下代码:
要了解如何迭代百里香叶中的对象列表,请查看这篇文章 使用 Spring 引导在百里香叶中迭代列表
Spring Boot - Thymeleaf - MySQL| Upload -- Download -- Files
Spring Boot - Thymeleaf - MySQL| Upload -- Download -- Files
现在,运行 UploadAndDownloadApplication 并转到 localhost:8888 并查看以下输出。
通过这种方式,我们学会了如何从/向数据库上传、下载文件。