背景
昨日与同事讨论问题过程中,他提到通过Jmeter无法上传文件,需要自己写Java代码。然后我回复了他,Jmeter是可以做到上传文件的。
下面我们自己动手写一个上传文件的接口,并通过Jmeter调用达到上传文件,并保存在本地。
编写上传文件接口
- 通过IDEA新建项目
- 选择
Spring Initializr
- 添加依赖如下
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 34 35 36 37 38 39 40 41
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
- 新建Java类
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package com.example.demo.controller;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.IOException; import java.util.Objects; import java.util.UUID;
@RestController public class FileController {
@PostMapping("/upload") @ResponseBody public String upload(@RequestParam("file") MultipartFile file, @RequestParam("name") String name) { String filename = ""; try { String uploadDir = "d:/file_spring/" + name + "/";
File dir = new File(uploadDir); if (!dir.exists()) { dir.mkdir(); } filename = executeUpload(uploadDir, file); } catch (Exception e) { e.printStackTrace(); } return filename; }
private String executeUpload(String uploadDir, MultipartFile file) throws Exception { int index = Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf("."); String suffix = file.getOriginalFilename().substring(index); String filename = UUID.randomUUID() + suffix; File serverFile = new File(uploadDir + filename);
if (!serverFile.exists()) { serverFile.getParentFile().mkdir(); try { serverFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } file.transferTo(serverFile); return filename; } }
|
通过Jmeter调用
1.添加线程组
2.添加HTTP请求
设置http

设置文件路径

- 查看结果
文件最终成功上传到本地
