卡帕加个岁的四大四大 阿森松

class RequestDemo{
    public static String convertImageToBase64Str(String imageFileName) {
        ByteArrayOutputStream baos = null;
        try {
            //获取图片类型
            String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);
            //构建文件
            File imageFile = new File(imageFileName);
            //通过ImageIO把文件读取成BufferedImage对象
            BufferedImage bufferedImage = ImageIO.read(imageFile);
            //构建字节数组输出流
            baos = new ByteArrayOutputStream();
            //写入流
            ImageIO.write(bufferedImage, suffix, baos);
            //通过字节数组流获取字节数组
            byte[] bytes = baos.toByteArray();
            //获取JDK8里的编码器Base64.Encoder转为base64字符
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url
     * 发送请求的 URL
     * @param jsonData
     * 请求参数,请求参数应该是Json格式字符串的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendJsonPost(String url, String jsonData) {
        StringBuilder sbf = new StringBuilder();
        try {
            //链接URL
            //url = URLEncoder.encode(url, "UTF-8");
            URL urlObject = new URL(url);
            //创建连接
            URLConnection uc = urlObject.openConnection();
            //uc.setRequestProperty("User-Agent", "Mozilla/4.76");
            HttpURLConnection conn = (HttpURLConnection) uc;
            conn.setRequestMethod("POST");
            conn.addRequestProperty("Content-Type", "application/json");
            //conn.setRequestProperty("connection", "Keep-Alive");
            //conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);

            //conn.setRequestProperty("Accept-Charset", "utf-8");
            //conn.setRequestProperty("contentType", "utf-8");

            //设置请求属性,缺少此语句报错Server returned HTTP response code: 403 for URL
            conn.connect();

            // String body = jsonData;
            // BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
            // writer.write(body);
            // writer.close();

            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            if (!"".equals(jsonData)) {
                out.writeBytes(jsonData);
            }
            out.flush();
            out.close();


            //读取返回结果集,此处记得设置编码格式为UTF-8,防止中文乱码
//             BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
//             String inputLine = null;
//             while ((inputLine = in.readLine()) != null) {
//                 json.append(inputLine);
//             }
//             in.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream()));
             
            
            String lines;
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                sbf.append(lines);
            }
            System.out.println(sbf);
            reader.close();

            conn.disconnect();
        } catch (MalformedURLException e) {
                 System.out.println("MalformedURLException");
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IOException e) {
                 System.out.println("IOException");
                // TODO Auto-generated catch block
            
                e.printStackTrace();
        }

        //将json数据以字符串的形式进行返回
        return sbf.toString();
    }
//java RequestDemo


    public static void main(String[] args){
        String img_path = "./me/test/pg/苹果黑星病 (423).JPG";
        String result  = convertImageToBase64Str(img_path);
        //System.out.println(result);
        // try{
        //     result = URLEncoder.encode(result, "utf-8");
        // }catch(Exception ex){
        //     System.out.println(ex.toString());
        // }
        String data = "{'key': ['image'], 'value': ['"+result+"']}";
        System.out.println(data);
        String ss = sendJsonPost("http://127.0.0.1:18082/detect/prediction",data);
        System.out.println(ss);
    }

}

?