zoukankan      html  css  js  c++  java
  • (jmeter笔记)导出响应内容的网络文件

    1.需求:打印响应内容的pdf文件

    2.场景设置:打印面单,导出响应数据中的pdf文件

      

     3.打印面单下添加BeanShell后置处理器 

     方法一:

        String url ="${Value}";
        
        // 构造URL
        URL weburl = new URL(url);
        // 打开连接
        URLConnection con = weburl.openConnection();
        // 设置请求超时
        con.setConnectTimeout(5 * 1000);
        // 输入流
        InputStream is = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        // 输出的文件流
        File sf = new File("G:/${SiOrderId}.pdf"); //导出路径
        OutputStream os = new FileOutputStream(sf);
            // 开始读取
        while ((len = is.read(bs)) != -1) {
         os.write(bs, 0, len);
        }
        // 完毕,关闭所有链接
        os.close();
        is.close();

    方法二:

    try {
        File file = new File("G:/${SiOrderId}.pdf"); // 文件保存路径
        String downloadUrl = "${Value}";  // 地址请求链接
        FileOutputStream fileOutputStream = new FileOutputStream(file);  // 文件写入流
        
        URL url = new URL(downloadUrl); // 实例化一个地址对象
        URLConnection connection = url.openConnection();    // 网络请求
        InputStream inputStream = connection.getInputStream();  // 获取请求响应流
        int length = 0;   // 缓冲成功读取长度
        byte[] bytes = new byte[1024];  // 数据缓冲区大小
        while ((length = inputStream.read(bytes)) != -1) {  // 循环从响应流读取字节
        fileOutputStream.write(bytes, 0, length);   // 循环将字节写入磁盘文件
        }
        fileOutputStream.close();   // 关闭文件写入流
        inputStream.close();    // 关闭请求响应流
        } catch (IOException e) {
        log.error("download error ! url :{}, exception:{}", downloadUrl, e);
    }

    转载请写明出处!!!

  • 相关阅读:
    layui + mvc + ajax 导出Excel功能
    PL/SQL Developer工具包和InstantClient连接Oracle 11g数据库
    .NET中JSON的序列化和反序列化的几种方式
    C# 编程中的堆栈(Stack)和队列(Queue)
    Oracle 数据库常用操作语句大全
    C#方法中参数ref和out的解析
    JS实现限行
    ajax+ashx 完美实现input file上传文件
    HTML5 学习
    Linux文件和目录操作管理命令
  • 原文地址:https://www.cnblogs.com/worldbugMsg/p/13259661.html
Copyright © 2011-2022 走看看