美文网首页
Rest-Assured get请求下载文件

Rest-Assured get请求下载文件

作者: 流年逝去sky | 来源:发表于2021-05-07 10:45 被阅读0次
     String scmIdc = "https://xx/invoicePDFDownload";
        String basePath = "C:\\Users\\xx\\src\\main\\resources\\";
    
        @Test
        public void downloadByInvoiceNo() throws IOException {
            List<String> invoiceList = MyUtils.readTxtFileAsList(basePath + "Invoices.txt");
            for (String invoiceNo : invoiceList) {
                InputStream inputStream = given().param("invoiceNumber", invoiceNo).get(scmIdc).asInputStream();
                MyUtils.transferInputStreamToFile(inputStream, new File(basePath + "invoice\\" + "invoice-" + invoiceNo + ".pdf"));
            }
        }
    
        @Test
        public void downloadByOrderNos() throws IOException {
            List<String> orderNoList = MyUtils.readTxtFileAsList(basePath + "Orders.txt");
            for (String orderNo : orderNoList) {
                Response response = queryInvoiceInfo(orderNo);
                String ret = response.path("ret");
                int totalCount = response.path("data.totalCount");
                if (ret.equals("1") && totalCount == 1) {
                    String invoiceStatus = response.path("data.result[0].invoiceState");
                    String invoiceNumber = response.path("data.result[0].invoiceNumber");
                    String bizNumber = response.path("data.result[0].bizNumber");
                    if (invoiceStatus.equals("开票成功")) {
                        InputStream inputStream = given().param("invoiceNumber", invoiceNumber).get(scmIdc).asInputStream();
                        MyUtils.transferInputStreamToFile(inputStream,
                                new File(basePath + "invoice\\" + "invoice-" + bizNumber + "-" + invoiceNumber + ".pdf"));
                    }
                } else {
                    System.out.println(orderNo + ": 发票异常");
                }
            }
        }
    
        public Response queryInvoiceInfo(String orderNO) {
            String url = " https://xx/queryInvoiceList";
            Map<String, String> cookie = new HashMap<>();
            cookie.put("xx", "xx");
            Map<String, String> header = new HashMap<>();
            header.put("xx", "xx");
            Map<String, Object> params = new HashMap<>();
            params.put("currentPage", 1);
            params.put("pageSize", 10);
            params.put("bizNumber", orderNO);
            return given().contentType("application/x-www-form-urlencoded; charset=UTF-8").cookies(cookie).headers(header).params(params).post(url);
        }
    

    MyUtils类

    import org.apache.commons.lang3.StringUtils;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyUtils {
        public static String formatJson(String jsonStr) {
            if (null == jsonStr || "".equals(jsonStr))
                return "";
            StringBuilder sb = new StringBuilder();
            char last = '\0';
            char current = '\0';
            int indent = 0;
            boolean isInQuotationMarks = false;
            for (int i = 0; i < jsonStr.length(); i++) {
                last = current;
                current = jsonStr.charAt(i);
                switch (current) {
                    case '"':
                        if (last != '\\') {
                            isInQuotationMarks = !isInQuotationMarks;
                        }
                        sb.append(current);
                        break;
                    case '{':
                    case '[':
                        sb.append(current);
                        if (!isInQuotationMarks) {
                            sb.append('\n');
                            indent++;
                            addIndentBlank(sb, indent);
                        }
                        break;
                    case '}':
                    case ']':
                        if (!isInQuotationMarks) {
                            sb.append('\n');
                            indent--;
                            addIndentBlank(sb, indent);
                        }
                        sb.append(current);
                        break;
                    case ',':
                        sb.append(current);
                        if (last != '\\' && !isInQuotationMarks) {
                            sb.append('\n');
                            addIndentBlank(sb, indent);
                        }
                        break;
                    default:
                        sb.append(current);
                }
            }
            return sb.toString();
        }
    
        private static void addIndentBlank(StringBuilder sb, int indent) {
            for (int i = 0; i < indent; i++) {
                sb.append('\t');
            }
        }
    
        // InputStream -> File
        public static void transferInputStreamToFile(InputStream inputStream, File file)
                throws IOException {
            try (FileOutputStream outputStream = new FileOutputStream(file)) {
                int read;
                byte[] bytes = new byte[1024];
                while ((read = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }
            }
        }
    
        public static List<String> readTxtFileAsList(String filePath) {
            List<String> list = new ArrayList<>();
            try {
                String encoding = "utf-8";
                File file = new File(filePath);
                if (file.isFile() && file.exists()) { //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while ((lineTxt = bufferedReader.readLine()) != null) {
                        String trimText = lineTxt.trim();
                        if (StringUtils.isNotBlank(trimText)) {
                            list.add(trimText);
                        }
                    }
                    read.close();
                } else {
                    System.out.println("找不到指定的文件");
                }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
            return list;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Rest-Assured get请求下载文件

          本文链接:https://www.haomeiwen.com/subject/qijsdltx.html