今天我们来学习使用Vue+SpringBoot来实现文件上传,其实今天的重点在于前端使用Vue获取文件,并传输到后台,至于后台上传文件则是通用的代码。
Vue获取文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <div id="app"> <input type="file" @change="getFile($event)"> <input type="button" value="上传" @click="upload()"> </div> </body> <script> const app = new Vue({ el: "#app", data: { formData: new FormData(), }, methods: { upload() { axios.post("/upload/image",this.formData).then(resp => { if (resp.status == 200) { alert("success"); } }); }, getFile(event) { let file = event.target.files[0]; let fileName = file.name; let index = fileName.lastIndexOf("."); if (index != -1) { let suffix = fileName.substr(index + 1).toLowerCase(); if (suffix == 'png' || suffix == 'jpg') { this.formData.append("file",file); } } } } }) </script>
|
后台项目依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
|
文件上传相关配置
application.yml
1 2 3 4 5 6 7 8
| file: upload: path: G:\temp\images\ allowTypes: - image/jpeg - image/png - image/jpg - image/bmp
|
UploadProperties.java
1 2 3 4 5 6 7 8 9 10 11 12
|
@Data @Component @ConfigurationProperties(prefix = "file.upload") public class UploadProperties { private String path; private List<String> allowTypes; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@Configuration public class UploadConfig {
@Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setMaxFileSize("10240KB"); factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); } }
|
文件上传后台代码
- controller层
1 2 3 4 5 6 7 8 9 10 11 12
| @RestController @RequestMapping("upload") public class UploadController {
@Autowired private UploadService uploadService;
@PostMapping("image") public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) throws Exception { return ResponseEntity.ok(uploadService.uploadImage(file)); } }
|
- service层接口
1 2 3 4 5 6 7 8 9 10
| public interface UploadService {
String uploadImage(MultipartFile file) throws Exception; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Service public class UploadServiceImpl implements UploadService {
@Autowired private UploadProperties uploadProperties;
@Override public String uploadImage(MultipartFile file) throws IOException { if(!uploadProperties.getAllowTypes().contains(file.getContentType())){ throw new IOException("文件上传类型错误!"); } String fileName = UploadUtils.generateFileName(file.getOriginalFilename()); file.transferTo(new File(uploadProperties.getPath()+fileName)); return fileName; } }
|
文件上传工具类
- IDUtils
1 2 3 4 5 6 7 8 9 10 11
| public class IDUtils {
public static String generateUniqueId() { return UUID.randomUUID().toString() + System.currentTimeMillis(); } }
|
- UploadUtils
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class UploadUtils {
public static String generateFileName(String oldName) { String suffix = oldName.substring(oldName.lastIndexOf(".")); return IDUtils.generateUniqueId() + suffix; } }
|