【新手】使用Spring上传图片和文件【自我满足】
环境
-
- Spring boot 2.1.5
-
- Java 1.8
-
- Intellij Community
-
- maven 4.0.0
-
- tomcat 9.0.19
- Mac OS
.
└── _src
└── _main
├── _java
| ├── _com.example.demo
| ├── FileUpdateController
| └── DemoApplication
└── _resources
├── _static
| ├── bootstrap.css
| ├── bootstrap.min.css
| └── training.css
├── _templates
| ├── uploadView.html
| └── uploadStatusView.html
└── application.propertiesTranslated Version:
目录
.
└── _src(源代码)
└── _main(主要代码)
├── _java(Java代码)
| ├── _com.example.demo(com.example.demo包)
| ├── FileUpdateController(文件更新控制器)
| └── DemoApplication(演示应用程序)
└── _resources(资源)
├── _static(静态文件)
| ├── bootstrap.css(引导样式)
| ├── bootstrap.min.css(压缩后的引导样式)
| └── training.css(培训样式)
├── _templates(模板文件)
| ├── uploadView.html(上传视图)
| └── uploadStatusView.html(上传状态视图)
└── application.properties(应用程序配置文件)
为了什么
实现图片和文件上传功能
内部内容或本质。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Spring Boot演示项目1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-configuration-processortrue
org.springframework.boot
spring-boot-starter-test
test
org.assertj
assertj-core
org.springframework.boot
spring-boot-maven-plugin
spring.thymeleaf.mode=HTML
spring.datasource.url=jdbc:mysql://localhost:3306/DB名?autoReconnect=true&
useSSL=false&serverTimezone=JST
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.servlet.multipart.max-file-size=15MB
spring.servlet.multipart.max-request-size=15MB
导入 com.example.demo.controller.FileUploadController;
导入 org.springframework.boot.SpringApplication;
导入 org.springframework.boot.autoconfigure.SpringBootApplication;
导入 org.springframework.context.annotation.ComponentScan;
导入 java.io.File;
@SpringBootApplication
@ComponentScan({“com.example.demo”, “controller”})
public class DemoApplication {
public static void main(String[] args) {
new File(FileUploadController.uploadDirectory).mkdir();
SpringApplication.run(DemoApplication.class, args);
}
}
包 com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@Controller
public class FileUploadController {
public static String uploadDirectory = System.getProperty(“user.dir”)+”uploads”;
@RequestMapping(“/”)
public String 上传页面(Model model){
return “上传视图”;
}
@RequestMapping(“/upload”)
public String 上传(Model model, @RequestParam(“files”) MultipartFile[] files){
StringBuilder 文件名 = new StringBuilder();
for(MultipartFile file : files){
Path 文件名和路径 = Paths.get(uploadDirectory, file.getOriginalFilename());
文件名.append(file.getOriginalFilename());
try {
Files.write(文件名和路径, file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
model.addAttribute(“msg”, “成功上传文件” + 文件名.toString());
return “上传状态视图”;
}
}
来源
YouTube: 使用Spring Boot上传文件和图片