javaSpring与Python的flask框架实现通讯

package com.example.demo.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import com.google.gson.Gson;

public class PythonProt {
    public String knnRegressor(Float[][] array,String Address) {
        try {
            // 构建要发送的二维数组
            // 使用 Gson 将二维数组转换为 JSON 字符串
            Gson gson = new Gson();
            String json = gson.toJson(array);
            // 设置 URL
            URL url = new URL(Address);
            // 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/json");
            // 启用输出流,以便我们可以发送数据
            connection.setDoOutput(true);
            // 获取输出流并写入 JSON 数据
            OutputStream os = connection.getOutputStream();
            os.write(json.getBytes());
            os.flush();
            os.close();
            // 发送请求并获取响应状态码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            // 打印响应内容
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public String NeuralNetwork(List<int[]> array, String Address) {
        try {
            // 构建要发送的二维数组
            // 使用 Gson 将二维数组转换为 JSON 字符串
            Gson gson = new Gson();
            String json = gson.toJson(array);
            // 设置 URL
            URL url = new URL(Address);
            // 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方法
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/json");
            // 启用输出流,以便我们可以发送数据
            connection.setDoOutput(true);
            // 获取输出流并写入 JSON 数据
            OutputStream os = connection.getOutputStream();
            os.write(json.getBytes());
            os.flush();
            os.close();
            // 发送请求并获取响应状态码
            int responseCode = connection.getResponseCode();
            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            // 打印响应内容
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值