package com.company;
// IntelliJ, How to add Jar File
// File\ProjectStructure\ProjectSettings\Modules\Dependencies\+\JARsOrDirectories\OK\OK
// import GSON.jar
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonElement;
// import HttpClient.jar (Apache Component)
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.entity.mime.StringBody;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ShinTest4{
public static void main(String[] args) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
// close 메소드가 포함된 HttpClient
try {
// 인스턴스가 더 이상 필요하지 않고 범위를 벗어나려고 할 때 연결 관리자를 호출하여 종료
HttpPost httppost = new HttpPost("http://210.217.95.183:8000/analyzer/");
// 연결할 url(이미지 인식 / 분석 )
FileBody bin = new FileBody(new File("/home/ehdrud1129/다운로드/OHaeYoung.jpg"));
// FileBody로 업로드할 이미지 경로 담기
StringBody comment = new StringBody("missoh.face, object", ContentType.TEXT_PLAIN);
// StringBody로 업로드할 텍스트 내용 담기
HttpEntity reqEntity = MultipartEntityBuilder.create()
// HttpEntity : Request, Response
.addPart("image", bin)
// Parameter image로 FileBody bin 담기
.addPart("modules", comment)
// Parameter modules로 StringBody comment 담기
.build();
// Multipart(HTTP를 통해 File을 Server로 전송하는 Content-type) build
httppost.setEntity(reqEntity);
// HttpEntity reqEntity 전송하기
System.out.println("executing request ");
// 실행 확인 콘솔
CloseableHttpResponse response = httpclient.execute(httppost);
// 전송한 httppost의 실행 결과를 받아오는 CloseableHttpResponse 객체 response 생성
try {
System.out.println("----------------------------------------");
System.out.println(response.getCode());
// response의 응답확인코드를 콘솔 확인
HttpEntity resEntity = response.getEntity();
// response의 엔티티 객체 생성
if (resEntity != null) {
// response 엔티티의 내용이 없지 않는 조건
String charset = "UTF-8";
System.out.println("Response content length: " + resEntity.getContentLength());
// response 엔티티의 String 길이 콘솔 확인
String content = EntityUtils.toString(response.getEntity(), charset);
// String type으로 변수 content 내용 담기
System.out.println(content);
JsonElement jelement = new JsonParser().parse(content);
// Parsing from String to JSON
JsonObject jobject = jelement.getAsJsonObject();
JsonArray jarray = jobject.getAsJsonArray("results");
// results[]
jobject = jarray.get(1).getAsJsonObject();
// results[1] : object
JsonArray objectArray = jobject.getAsJsonArray("module_result");
String object1 = "object{";
String description1 = "";
String score1 = "";
String y1 = "";
String h1 = "";
String w1 = "";
String x1 = "";
for (int i = 0; i < objectArray.size(); i++) {
jobject = objectArray.get(i).getAsJsonObject();
JsonArray labelArray = jobject.getAsJsonArray("label");
// results[0 : label
JsonObject labelObject = labelArray.get(0).getAsJsonObject();
description1 = labelObject.get("description").getAsString();
// description
score1 = labelObject.get("score").getAsString();
// score
JsonObject positionObject = jobject.getAsJsonObject("position");
// position
y1 = positionObject.get("y").getAsString();
// y, h, w, x
h1 = positionObject.get("h").getAsString();
w1 = positionObject.get("w").getAsString();
x1 = positionObject.get("x").getAsString();
object1 = object1 + "[" + description1 + "," + score1 + "," + y1 +
"," + h1 + "," + w1 + "," + x1 + "]";
}
object1 = object1 + "}";
System.out.println(object1);
jobject = jarray.get(0).getAsJsonObject();
// results[0] : face
JsonArray faceArray = jobject.getAsJsonArray("module_result");
String face0 = "face{";
String description0 = "";
String score0 = "";
String y0 = "";
String h0 = "";
String w0 = "";
String x0 = "";
for (int i = 0; i < faceArray.size(); i++) {
jobject = faceArray.get(i).getAsJsonObject();
JsonArray labelArray = jobject.getAsJsonArray("label");
// Array label[0]
JsonObject labelObject = labelArray.get(0).getAsJsonObject();
description0 = labelObject.get("description").getAsString();
// description
score0 = labelObject.get("score").getAsString();
// score
JsonObject positionObject = jobject.getAsJsonObject("position");
// position
y0 = positionObject.get("y").getAsString();
// y, h, w, x
h0 = positionObject.get("h").getAsString();
w0 = positionObject.get("w").getAsString();
x0 = positionObject.get("x").getAsString();
face0 = face0 + "[" + description0 + "," + score0 + "," + y0 + "," +
h0 + "," + w0 + "," + x0 + "]";
}
face0 = face0 + "}";
System.out.println(face0);
BufferedOutputStream bs = null;
// object 텍스트
try {
bs = new BufferedOutputStream(new FileOutputStream("/home/ehdrud1129/
다운로드/20200625_httprequest_object_노동경.txt"));
bs.write(object1.getBytes());
} catch (Exception e) {
e.getStackTrace();
// TODO: handle exception
} finally {
bs.close();
}
BufferedOutputStream bs2 = null;
// face 텍스트
try {
bs2 = new BufferedOutputStream(new FileOutputStream("/home/ehdrud1129
/다운로드/20200625_httprequest_face_노동경.txt"));
bs2.write(face0.getBytes());
} catch (Exception e) {
e.getStackTrace();
// TODO: handle exception
} finally {
bs2.close();
}
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} catch (IOException | ParseException e) {
e.printStackTrace();
} finally {
httpclient.close();
}
}
}