文章目錄
- 一、前言
- 二、預備知識
- 1、構造方法
- 2、創建功能
- 3、重命名和洗掉功能
- 4、獲取功能
- 三、Java 實作檔案上傳下載功能
- 1、服務下載代碼
- 2、前端代碼
- 3、運行效果
- 四、JMeter 檔案上傳與下載腳本撰寫
- 1、檔案上傳腳本
- 2、檔案下載腳本
- 五、總結
一、前言
性能測驗作業中,檔案上傳也是經常見的性能壓測場景之一,那么 JMeter 檔案上傳下載腳本怎么做?
知識點:
- Java 實作檔案上傳下載功能
- JMeter 檔案上傳與下載腳本撰寫
二、預備知識
先學習下 Java API 關于檔案操作的 API:
1、構造方法
-
File(File parent, String child):根據 parent 抽象路徑名和 child 路徑名字串創建一個新 File 實體,
-
File(String pathname):通過將給定路徑名字串轉換為抽象路徑名來創建一個新 File 實體,
-
File(String parent, String child):根據 parent 路徑名字串和 child 路徑名字串創建一個新 File 實體,
-
File(URI uri):通過將給定的 file URI 轉換為一個抽象路徑名來創建一個新的 File 實體,
-
public boolean createNewFile():創建檔案 如果存在這樣的檔案,就不創建了
2、創建功能
-
public boolean mkdir():創建檔案夾 如果存在這樣的檔案夾,就不創建了
-
public boolean mkdirs():創建檔案夾,如果父檔案夾不存在,會幫你創建出來
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():獲取長度,位元組數
- public long lastModified():獲取最后一次的修改時間,毫秒值
- public String[] list():獲取指定目錄下的所有檔案或者檔案夾的名稱陣列
- public File[] listFiles():獲取指定目錄下的所有檔案或者檔案夾的File陣列
三、Java 實作檔案上傳下載功能
1、服務下載代碼
/**
* @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\\";
// 檔案重命名,防止重復
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;
}
}
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標簽*必須*放在最前面,任何其他內容都*必須*跟隨其后! -->
<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>
3、運行效果

四、JMeter 檔案上傳與下載腳本撰寫
打開 Jmeter 并且創建執行緒組、http 請求,
1、檔案上傳腳本


注意:

驗證結果:


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();
五、總結
以上只是簡單介紹,知識點很多涉及 Java 檔案操作,目錄操作,http 請求等資訊,
文章原始碼:
- https://github.com/zuozewei/blog-example/tree/master/Performance-testing/01-test-tool/jmeter/file/sdechartsjs
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/289554.html
標籤:其他
