性能测试工作中,文件上传也是经常见的性能压测任务之一,之前工作中一直使用loadrunner做文件上传与下载压测,近几年开始使用Jmeter做性能测试,那么文件上传下载脚本怎么做,今天分享Jmeter文件上传与下载脚本怎么做.
先学习下java Api关于文件操作的API,这样方便咱们进一步工作
/** * @author liwen * @Title: FileController * @Description: 文件操作类 * @date 2019/12/13 / 21:00 */@Controller@RequestMapping("/file/")public class FileController {
/** * 文件上传 * * @param fileupload 文件 * @return msg */ @PostMapping("fileupload") @ResponseBody public Msg upload(@RequestParam("fileupload") MultipartFile fileupload) {
if (fileupload.isEmpty() || fileupload.getSize() < 0) { return Msg.fail().add("mgs", "文件为空,上传失败!"); } // 获取文件名字 String fileName = fileupload.getOriginalFilename(); // 获取后缀名 // String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 文件保存路径 String filePath = "E:\\test\\7d\\upload\\"; // 文件重命名,防止重复 fileName = filePath + UUID.randomUUID() + fileName; // 文件对象 File dest = new File(fileName); // 判断路径是否存在,如果不存在则创建 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } try { // 保存到服务器中 fileupload.transferTo(dest); return Msg.success().add("mgs", "文件上传成功"); } catch (Exception e) { e.printStackTrace(); } return Msg.fail().add("mgs", "文件上传失败"); }
/** * 文件下载 * * @param name 下载文件名字 * @param response 响应流 * @return mgs * @throws Exception 异常处理 */ @GetMapping("download") public void download(@RequestParam("filedown") String name, HttpServletResponse response) throws Exception { if (name.isEmpty()) { return; } // 文件地址,真实环境是存放在数据库表中 File file = new File("E:\\test\\7d\\upload\\" + name); //判断文件是否存在 if (!file.exists()) { return; } // 文件对象输入流 FileInputStream fis = new FileInputStream(file); // 设置相关格式 response.setContentType("application/force-download"); // 设置下载后的文件名以及header response.addHeader("Content-disposition", "attachment;fileName=" + name); // 创建输出对象 OutputStream os = response.getOutputStream(); // 常规操作 byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { os.write(buf, 0, len); } fis.close(); return; }
}
<!DOCTYPE html><html lang="zh-CN" xmlns:th="http://www.thymeleaf.org"><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后!--> <title>文件上传下载</title> <meta name="description" content="文件上传下载"> <meta name="author" content="liwen"> <!-- Bootstrap --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 --> <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 --> <!--[if lt IE 9]> <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script> <![endif]--></head><body><div class="container"> <h1>你好,我好,大家好!</h1> <br> <div> <h2>文件上传</h2> <form id="fileupload" enctype='multipart/form-data'> <input type='file' name='fileupload'> <button type='button' class="btn btn-primary" onclick="uploadFile()">上传</button> </form> </div>
<div> <h2>文件下载</h2> <form th:action="@{/file/download}" action="/file/download" method="get"> <input type='text' name='filedown'> <button type='submit' class="btn btn-primary">下传</button> </form> </div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) --> <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script> <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。--> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script></div></body><script>
//文件上传 function uploadFile() { //FormData是html5的接口,使用它一行代码便可以拿到整个form表单对象: var form = new FormData(document.getElementById("fileupload")); $.ajax({ url: "/file/fileupload", type: "post", data: form, cache: false, processData: false, contentType: false, success: function (data) { if (data.code == 100) { alert(data.extend.mgs); } else { alert(data.extend.mgs); } }, error: function (e) { alert("网络错误,请重试!!"); } }); }
</script></html>
-打开Jmeter并且创建线程组、http请求
import java.io.*;byte[] result = prev.getResponseData();String file_name = "E:\\test\\7d\\data\\2222.ico";File file = new File(file_name);FileOutputStream out = new FileOutputStream(file);out.write(result);out.close();
上面是简单的介绍,知识点很多java文件操作,目录操作,http请求等信息,高老师经典一句《看到数据,推断什么地方有性能问题,背后的知识点需要积累多少日月》。