欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java性能工具JMeter實(shí)現(xiàn)上傳與下載腳本編寫

 更新時(shí)間:2021年07月22日 10:56:26   作者:zuozewei  
性能測(cè)試工作中,文件上傳也是經(jīng)常見的性能壓測(cè)場(chǎng)景之一,那么 JMeter 文件上傳下載腳本怎么做,本文詳細(xì)的來介紹一下,感興趣的可以了解一下

一、前言

性能測(cè)試工作中,文件上傳也是經(jīng)常見的性能壓測(cè)場(chǎng)景之一,那么 JMeter 文件上傳下載腳本怎么做?

知識(shí)點(diǎn):

  • Java 實(shí)現(xiàn)文件上傳下載功能
  • JMeter 文件上傳與下載腳本編寫

二、預(yù)備知識(shí)

先學(xué)習(xí)下 Java API 關(guān)于文件操作的 API:

1、構(gòu)造方法

  • File(File parent, String child):根據(jù) parent 抽象路徑名和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例。
  • File(String pathname):通過將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建一個(gè)新 File 實(shí)例。
  • File(String parent, String child):根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個(gè)新 File 實(shí)例。
  • File(URI uri):通過將給定的 file URI 轉(zhuǎn)換為一個(gè)抽象路徑名來創(chuàng)建一個(gè)新的 File 實(shí)例。
  • public boolean createNewFile():創(chuàng)建文件 如果存在這樣的文件,就不創(chuàng)建了

2、創(chuàng)建功能

  • public boolean mkdir():創(chuàng)建文件夾 如果存在這樣的文件夾,就不創(chuàng)建了
  • public boolean mkdirs():創(chuàng)建文件夾,如果父文件夾不存在,會(huì)幫你創(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():獲取絕對(duì)路徑
  • public String getPath():獲取路徑
  • public String getName():獲取名稱
  • public long length():獲取長(zhǎng)度。字節(jié)數(shù)
  • public long lastModified():獲取最后一次的修改時(shí)間,毫秒值
  • public String[] list():獲取指定目錄下的所有文件或者文件夾的名稱數(shù)組
  • public File[] listFiles():獲取指定目錄下的所有文件或者文件夾的File數(shù)組

三、Java 實(shí)現(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;
        // 文件對(duì)象
        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;
        }
        // 文件地址,真實(shí)環(huán)境是存放在數(shù)據(jù)庫表中
        File file = new File("E:\\test\\7d\\upload\\" + name);
        //判斷文件是否存在
        if (!file.exists()) {
            return;
        }
        // 文件對(duì)象輸入流
        FileInputStream fis = new FileInputStream(file);
        // 設(shè)置相關(guān)格式
        response.setContentType("application/force-download");
        // 設(shè)置下載后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + name);
        // 創(chuàng)建輸出對(duì)象
        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個(gè)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 頁面拖拽到瀏覽器中)訪問頁面時(shí) 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ù)需要只加載單個(gè)插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</div>
</body>
<script>

    //文件上傳
    function uploadFile() {
        //FormData是html5的接口,使用它一行代碼便可以拿到整個(gè)form表單對(duì)象:
        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ò)錯(cuò)誤,請(qǐng)重試??!");
            }
        });
    }

</script>
</html>

3、運(yùn)行效果

在這里插入圖片描述

四、JMeter 文件上傳與下載腳本編寫

打開 Jmeter 并且創(chuàng)建線程組、http 請(qǐng)求。

1、文件上傳腳本

在這里插入圖片描述

在這里插入圖片描述

注意:

在這里插入圖片描述

驗(yàn)證結(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é)

以上只是簡(jiǎn)單介紹,知識(shí)點(diǎn)很多涉及 Java 文件操作,目錄操作,http 請(qǐng)求等信息。

文章源碼:

https://github.com/zuozewei/blog-example/tree/master/Performance-testing/01-test-tool/jmeter/file/sdechartsjs

到此這篇關(guān)于Java性能工具JMeter實(shí)現(xiàn)上傳與下載腳本編寫的文章就介紹到這了,更多相關(guān)JMeter上傳與下載腳本編寫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論