Java性能工具JMeter實現(xiàn)上傳與下載腳本編寫
一、前言
性能測試工作中,文件上傳也是經(jīng)常見的性能壓測場景之一,那么 JMeter 文件上傳下載腳本怎么做?
知識點:
- Java 實現(xiàn)文件上傳下載功能
- JMeter 文件上傳與下載腳本編寫
二、預(yù)備知識
先學(xué)習(xí)下 Java API 關(guān)于文件操作的 API:
1、構(gòu)造方法
- File(File parent, String child):根據(jù) parent 抽象路徑名和 child 路徑名字符串創(chuàng)建一個新 File 實例。
- File(String pathname):通過將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建一個新 File 實例。
- File(String parent, String child):根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個新 File 實例。
- File(URI uri):通過將給定的 file URI 轉(zhuǎn)換為一個抽象路徑名來創(chuàng)建一個新的 File 實例。
- public boolean createNewFile():創(chuàng)建文件 如果存在這樣的文件,就不創(chuàng)建了
2、創(chuàng)建功能
- public boolean mkdir():創(chuàng)建文件夾 如果存在這樣的文件夾,就不創(chuàng)建了
- public boolean mkdirs():創(chuàng)建文件夾,如果父文件夾不存在,會幫你創(chuàng)建出來
3、重命名和刪除功能
- public boolean renameTo(File dest):把文件重命名為指定的文件路徑
- public boolean isDirectory():判斷是否是目錄
- public boolean isFile():判斷是否是文件
- public boolean exists():判斷是否存在
- public boolean canRead():判斷是否可讀
- public boolean canWrite():判斷是否可寫
- public boolean isHidden():判斷是否隱藏
4、獲取功能
- public String getAbsolutePath():獲取絕對路徑
- public String getPath():獲取路徑
- public String getName():獲取名稱
- public long length():獲取長度。字節(jié)數(shù)
- public long lastModified():獲取最后一次的修改時間,毫秒值
- public String[] list():獲取指定目錄下的所有文件或者文件夾的名稱數(shù)組
- public File[] listFiles():獲取指定目錄下的所有文件或者文件夾的File數(shù)組
三、Java 實現(xiàn)文件上傳下載功能
1、服務(wù)下載代碼
/** * @author 7d * @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\\"; // 文件重命名,防止重復(fù) fileName = filePath + UUID.randomUUID() + fileName; // 文件對象 File dest = new File(fileName); // 判斷路徑是否存在,如果不存在則創(chuàng)建 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } try { // 保存到服務(wù)器中 fileupload.transferTo(dest); return Msg.success().add("mgs", "文件上傳成功"); } catch (Exception e) { e.printStackTrace(); } return Msg.fail().add("mgs", "文件上傳失敗"); } /** * 文件下載 * * @param name 下載文件名字 * @param response 響應(yīng)流 * @return mgs * @throws Exception 異常處理 */ @GetMapping("download") public void download(@RequestParam("filedown") String name, HttpServletResponse response) throws Exception { if (name.isEmpty()) { return; } // 文件地址,真實環(huán)境是存放在數(shù)據(jù)庫表中 File file = new File("E:\\test\\7d\\upload\\" + name); //判斷文件是否存在 if (!file.exists()) { return; } // 文件對象輸入流 FileInputStream fis = new FileInputStream(file); // 設(shè)置相關(guān)格式 response.setContentType("application/force-download"); // 設(shè)置下載后的文件名以及header response.addHeader("Content-disposition", "attachment;fileName=" + name); // 創(chuàng)建輸出對象 OutputStream os = response.getOutputStream(); // 常規(guī)操作 byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { os.write(buf, 0, len); } fis.close(); return; } }
2、前端代碼
<!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標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! --> <title>文件上傳下載</title> <meta name="description" content="文件上傳下載"> <meta name="author" content="liwen"> <!-- Bootstrap --> <link rel="stylesheet"> <!-- HTML5 shim 和 Respond.js 是為了讓 IE8 支持 HTML5 元素和媒體查詢(media queries)功能 --> <!-- 警告:通過 file:// 協(xié)議(就是直接將 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 插件。你也可以根據(jù)需要只加載單個插件。 --> <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("網(wǎng)絡(luò)錯誤,請重試??!"); } }); } </script> </html>
3、運行效果
四、JMeter 文件上傳與下載腳本編寫
打開 Jmeter 并且創(chuàng)建線程組、http 請求。
1、文件上傳腳本
注意:
驗證結(jié)果:
2、文件下載腳本
參考代碼:
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();
五、總結(jié)
以上只是簡單介紹,知識點很多涉及 Java 文件操作,目錄操作,http 請求等信息。
文章源碼:
到此這篇關(guān)于Java性能工具JMeter實現(xiàn)上傳與下載腳本編寫的文章就介紹到這了,更多相關(guān)JMeter上傳與下載腳本編寫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)類型之細(xì)講char類型與編碼關(guān)系
這幾天一直在復(fù)習(xí)Java基礎(chǔ)知識,特地寫了一篇文章來做一下筆記,文中有非常詳細(xì)的圖文示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05java編程FinalReference與Finalizer原理示例詳解
這篇文章主要為大家介紹了java編程FinalReference與Finalizer的核心原理以及示例源碼的分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01java實現(xiàn)去除ArrayList重復(fù)字符串
本文主要介紹了java實現(xiàn)去除ArrayList重復(fù)字符串,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09SpringCloud開啟session共享并存儲到Redis的實現(xiàn)
這篇文章主要介紹了SpringCloud開啟session共享并存儲到Redis的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02spring-boot集成spring-security的oauth2實現(xiàn)github登錄網(wǎng)站的示例
本篇文章主要介紹了spring-boot集成spring-security的oauth2實現(xiàn)github登錄網(wǎng)站的示例,非常具有實用價值,需要的朋友可以參考下2017-10-10Intellij IDEA實現(xiàn)SpringBoot項目多端口啟動的兩種方法
有時候使用springboot項目時遇到這樣一種情況,用一個項目需要復(fù)制很多遍進(jìn)行測試,除了端口號不同以外,沒有任何不同。遇到這種情況怎么辦呢?這時候可以使用Intellij IDEA解決2018-06-06