下面是详细讲解“Spring Boot搭建文件上传服务的方法”的完整攻略。
1. 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 文件上传依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
2. 配置文件上传参数
在application.yml文件中添加如下配置:
spring:
servlet:
multipart:
# 设定总上传数据的最大值,默认为1MB
max-file-size: 50MB
# 设定单个上传文件的最大值,默认为10KB
max-request-size: 10MB
# 设定上传时,文件的临时存放路径,默认为:java.io.tmpdir
location: /tmp
3. 文件上传API接口
import java.io.File;
import java.io.IOException;
import javax.validation.constraints.NotNull;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
@Validated
public class FileController {
private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);
/**
* 文件上传
*
* @param file 上传的文件对象
* @return 文件上传的结果
*/
@PostMapping(value = "/upload")
public String upload(@NotNull(message = "文件不能为空") @RequestParam(value = "file") MultipartFile file) {
LOGGER.info("start to upload file.");
String format = DateFormatUtils.format(System.currentTimeMillis(), "yyyyMMddHHmmss");
String fileUrl = null;
try {
//获取根目录
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) {
path = new File("");
}
File upload = new File(path.getAbsolutePath(), "static/upload/");
LOGGER.info("upload path: {}", upload.getAbsolutePath());
//生成文件名
String originalFilename = file.getOriginalFilename();
String fileSuffix = FilenameUtils.getExtension(originalFilename);
String newFileName = format + "." + fileSuffix;
//上传文件
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(upload, newFileName));
fileUrl = "/upload/" + newFileName;
LOGGER.info("upload success! fileUrl: {}", fileUrl);
} catch (IOException e) {
LOGGER.error("upload failed: {}", e.getMessage());
return "failed";
}
return fileUrl;
}
}
4. 文件上传页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传示例</title>
</head>
<body>
<h3>文件上传示例</h3>
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="file">选择文件:</label>
<input type="file" id="file" name="file"/><br/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
通过上述4个步骤,我们就可以在Spring Boot中搭建起一个文件上传的服务。
示例说明:
- 上传文件总大小为50MB,单个文件大小不超过10MB;
- 文件默认存储在服务器的/static/upload/路径下;
- 提供了一个文件上传的API接口,将上传结果返回到前端页面。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring Boot搭建文件上传服务的方法 - Python技术站