sptingboot 图片上传 的实验内容和步骤
时间: 2024-02-29 16:55:06 浏览: 28
好的,针对您的问题,以下是一个简单的Spring Boot图片上传的实验内容和步骤:
1. 创建一个Spring Boot项目,并添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
```
2. 创建一个控制器类,并添加以下代码:
```java
@Controller
public class UploadController {
@GetMapping("/")
public String index() {
return "upload";
}
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
try {
FileUploadUtil.saveFile(fileName, file);
redirectAttributes.addFlashAttribute("message", "The file has been uploaded successfully!");
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/";
}
}
```
3. 创建一个工具类FileUploadUtil,用于保存上传的图片:
```java
public class FileUploadUtil {
public static void saveFile(String fileName, MultipartFile file) throws IOException {
Path uploadDir = Paths.get("uploads");
if (!Files.exists(uploadDir)) {
Files.createDirectories(uploadDir);
}
try (InputStream inputStream = file.getInputStream()) {
Path filePath = uploadDir.resolve(fileName);
Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IOException("Could not save uploaded file: " + fileName);
}
}
}
```
4. 创建一个HTML文件upload.html,用于显示上传表单和上传成功的消息:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>File Upload Example</title>
</head>
<body>
<h2>File Upload Example</h2>
<div th:if="${message}" class="alert alert-success" th:text="${message}"></div>
<form method="POST" enctype="multipart/form-data" action="/upload">
<div>
<input type="file" name="file" />
</div>
<br/>
<div>
<button type="submit">Upload</button>
</div>
</form>
</body>
</html>
```
5. 运行应用程序,并在浏览器中访问http://localhost:8080/,即可看到上传表单。选择一张图片并上传,上传成功后将显示成功消息。
以上就是一个简单的Spring Boot图片上传的实验内容和步骤。希望能对您有所帮助。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)