객체를 직렬화하고 httpserver에서 전송해야합니다. 서버에서 클라이언트로 문자열을 보내는 방법을 이미 알고 있지만 어떻게 해야할지 모르겠습니다.HttpHandler 클래스에서 객체를 직렬화하는 방법
public class Test {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/test", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String response = "This is the response";
//this part here shows how to send a string
//but i need to send an object here
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
그래서 난 구글하지만 검색 결과로했는데, 나는 기계적으로 HttpServer에가에 있어요에 사용하지 않을 것을 메신저로하고 메신저도 모른 채 (코드를 변경하려고 :
개체 그래서 나는이 코드를 java) 이 방법은 :
하지만 일식은 나에게 ObjectOutputStream에() 생성자이 보이지 않는 그의 HttpExchange이 인수 적용되지 않습니다 하더군요 오류 (INT, ObjectInputStream에) 나는이 문제를 해결할 수있는 방법을
당신이 어떤 생각을 가지고 있습니까를 보여줍니다 ? 도움을 주셔서 감사합니다.
나는 그것을 "http 또는 다른 출력 스트림을 여기에"obj 함께 변경 한 짓을하지만 나에게 OutputStream을 인스턴스화 할 수 없다는 오류를 보여줍니다. –