通过返回响应获取响应数据及响应文件

通过返回响应获取响应数据及响应文件

1.获取相应文件

try {
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            // 处理项目2返回的响应结果
            if (entity != null) {
                // 在这里处理项目2返回的响应结果
                String contentType = entity.getContentType().getValue();

                // 判断响应是否为图片类型
                if (contentType.startsWith("image/")) {
                    try (InputStream inputStream = entity.getContent()) {
                        // 创建临时文件
                        File tempFile = File.createTempFile(fileName.split("\\.")[0], "."+fileName.split("\\.")[1]);

                        // 使用文件输出流将响应体写入临时文件
                        try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            while ((bytesRead = inputStream.read(buffer)) != -1) {
                                outputStream.write(buffer, 0, bytesRead);
                            }
                        }

                        // 返回临时文件
                        return tempFile;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    // 如果响应不是图片类型,可以根据实际情况处理其他数据类型的响应结果
                    String responseString = EntityUtils.toString(entity, "UTF-8");
                    // 在这里处理其他数据类型的响应结果
                }


            }
        } catch (IOException e) {
            e.printStackTrace();
        }

2.获取相应数据

try {
            // 发送请求并获取响应
            HttpResponse response0 = httpClient.execute(httpPost);

            // 处理响应...
            StatusLine statusLine = response0.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            HttpEntity httpEntity = response0.getEntity();

            if (httpEntity != null && statusCode == 200) {
                // 使用 InputStream 或其他方法读取响应体数据
                InputStream inputStream = httpEntity.getContent();

                // 处理响应体数据...
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                StringBuilder responseStringBuilder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    responseStringBuilder.append(line);
                }
                reader.close();
                inputStream.close();

                // 输出响应结果
                System.out.println("Response: " + responseStringBuilder.toString());

                JSONObject jsonObject = JSONObject.parseObject(responseStringBuilder.toString());
                newFileName = jsonObject.getString("data");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭HTTP连接
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值