我正在做一个文字游戏,需要将一个对象(地方)上传到Java Web服务器。(玩家需要共享数据,但是这里不考虑安全性)我正在使用java.net.http API。我目前的方法是
public void sendNewPlace(Place place) {
HttpRequest.BodyPublisher publisher =
HttpRequest.BodyPublishers.ofInputStream(() -> {
PipedInputStream in = new PipedInputStream();
ForkJoinPool.commonPool().submit(() -> {
try (PipedOutputStream out = new PipedOutputStream(in)) {
return place;
}
});
return in;
});
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:410/Places/Send/"))
.POST(publisher)
.build();
try {
client.send(request, HttpResponse.BodyHandlers.discarding());
} catch (IOException | InterruptedException e) {
System.out.println("Error: " + e + ", please contact game admin.");
}
}
但这引发java.io.IOException: chunked transfer encoding, state: READING_LENGTH
了client.send()
。我真的不知道该怎么做才能解决此问题,所有教程和StackOverflow问题要么使用大型的第三方API,过时的代码(Java 9之前的版本),要么想要发送String或int或其他内容。都非常无益。我不太确定这段代码的作用,我在一个教程或问题中找到了它并对其进行了修改。我想我不想尝试使用JSON,并且我试图不使用3rd-party库。如果您想知道,我的服务器代码如下:
public void makePlacesRecieveContext() {
server.createContext("/Places/Send/", new HttpHandler() {
public void handle(HttpExchange exchange) throws IOException {
System.out.println("Hit! /Places/Send/");
exchange.sendResponseHeaders(200, 0);
ObjectInputStream out = new ObjectInputStream(exchange.getRequestBody());
try {
Place pl = (Place)out.readObject();
GetResponces.writeFile(new File("C://Users//programmerGuy//gameServer//Places//" + pl.getName()),pl);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
out.close();
exchange.getRequestBody().close();
System.out.println(exchange.getRemoteAddress().getHostName() + " : " + exchange.getRemoteAddress().getHostString());
}
});
}
GetResponces是我构建的用于处理大多数文件IO的类。我需要做的就是能够将这些Place对象上传到服务器,我的问题是,我该怎么做?
编辑:我的新客户端代码是:
public void sendNewPlace(Place place) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://127.0.0.1:410/Places/Send/"))
.POST(BodyPublishers.ofString(new Gson().toJson(place)))
.build();
try {
client.send(request, HttpResponse.BodyHandlers.discarding());
} catch (IOException | InterruptedException e) {
System.out.println("Error: " + e + ", please contact game admin.");
}
}
如果服务器端代码与之有关,则为:
public void makePlacesRecieveContext() {
server.createContext("/Places/Send/", new HttpHandler() {
public void handle(HttpExchange exchange) throws IOException {
System.out.println("Hit! /Places/Send/");
exchange.sendResponseHeaders(200, 0);
String byteString = new String(exchange.getRequestBody().readAllBytes());
Place p = new Gson().fromJson(byteString, Place.class);
GetResponces.writeFile(new File(System.getProperty("user.home") + "/webServer/places/" + p.getName() + ".place"), p);
System.out.println(exchange.getRemoteAddress().getHostName() + " : " + exchange.getRemoteAddress().getHostString());
}
});
}
由于某些原因,我的错误保持不变...我的客户端代码为println-ingError: java.io.IOException: chunked transfer encoding, state: READING_LENGTH, please contact game admin.
编辑3:完整的错误如下:(我尝试使用破坏器,希望它能起作用)java.io.IOException:分块传输编码,状态:java.net.http / jdk.internal.net.http上的READING_LENGTH。 txtGame上的HttpClients.HttpPlaceClient.sendNewPlace(HttpPlaceClient.java:53)上的java.net.http / jdk.internal.net.http.HttpClientFacade.send(HttpClientFacade.java:119)上的HttpClientImpl.send(HttpClientImpl.java:565) txtGame.Player的.Modules.makeNewPlace(Modules.java:117)。txtGame.Main的(Player.java:38)(Main.java:16)txtGame.Main.main(Main.java:99) :java.io.IOException:分块传输编码,状态:java.net.http / jdk上的READING_LENGTH,位于java.net.http / jdk.internal.net.http.common.Utils.wrapWithExtraDetail(Utils.java:294)。位于java.net.http / jdk.internal.net.http的internal.net.http.Http1Response $ BodyReader.onReadError(Http1Response.java:741)。java.net.http / jdk.internal.net.http.Http1AsyncReceiver.flush(Http1AsyncReceiver.java:263)处的Http1AsyncReceiver.checkForErrors(Http1AsyncReceiver.java:297),位于java.net.http / jdk.internal.net.http。 java.net.http / jdk.internal.net.http.common.SequentialScheduler $ CompleteRestartableTask.run(SequentialScheduler.java:147)上的common.SequentialScheduler $ SynchronizedRestartableTask.run(SequentialScheduler.java:175)在java.net.http / java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)上的jdk.internal.net.http.common.SequentialScheduler $ SchedulableTask.run(SequentialScheduler.java:198)。 util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:628)at java.base / java.lang.Thread.run(Thread.java:834)原因:java.io.EOFException:在Java读取时到达EOF .net.http / jdk.internal.net.http。java.net.http / jdk.internal.net.http.SocketTube $ InternalReadPublisher $ ReadSubscription.signalCompletion(SocketTube.java:632)上的Http1AsyncReceiver $ Http1TubeSubscriber.onComplete(Http1AsyncReceiver.java:591)。位于java.net.http / jdk.internal.net.http.SocketTube $ SocketFlowTask.run(SocketTube.java:175)的internal.net.http.SocketTube $ InternalReadPublisher $ InternalReadSubscription.read(SocketTube.java:833)。 net.http / jdk.internal.net.http.common.SequentialScheduler $ SchedulableTask.run(SequentialScheduler.java:198)在java.net.http / jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java :271),位于java.net.http / jdk.internal.net.http.common.SequentialScheduler.runOrSchedule(SequentialScheduler.java:224),位于java.net.http / jdk.internal.net.http.SocketTube $ InternalReadPublisher $ InternalReadSubscription 。java.net.http / jdk.internal.net.http.SocketTube $ InternalReadPublisher $ ReadEvent.signalEvent(SocketTube.java:941)处的signalReadable(SocketTube.java:763)在java.net.http / jdk.internal.net处。 java.net.http / jdk.internal.net.http.HttpClientImpl $ SelectorManager.handleEvent(HttpClientImpl.java:957)上的http.SocketTube $ SocketFlowEvent.handle(SocketTube.java:245)。 internal.net.http.HttpClientImpl $ SelectorManager.lambda $ run $ 3(HttpClientImpl.java:912)在java.base / java.util.ArrayList.forEach(ArrayList.java:1541)在java.net.http / jdk.internal .net.http.HttpClientImpl $ SelectorManager.run(HttpClientImpl.java:912)245),位于java.net.http / jdk.internal.net.http.HttpClientImpl $ SelectorManager.handleEvent(HttpClientImpl.java:957),位于java.net.http / jdk.internal.net.http.HttpClientImpl $ SelectorManager.lambda $在java.base / java.util.ArrayList.forEach(ArrayList.java:1541)处运行$ 3(HttpClientImpl.java:912)在java.net.http / jdk.internal.net.http.HttpClientImpl $ SelectorManager.run(HttpClientImpl .java:912)245),位于java.net.http / jdk.internal.net.http.HttpClientImpl $ SelectorManager.handleEvent(HttpClientImpl.java:957),位于java.net.http / jdk.internal.net.http.HttpClientImpl $ SelectorManager.lambda $在java.base / java.util.ArrayList.forEach(ArrayList.java:1541)处运行$ 3(HttpClientImpl.java:912)在java.net.http / jdk.internal.net.http.HttpClientImpl $ SelectorManager.run(HttpClientImpl .java:912)
注意!!!:任何使这个问题更短的帮助(可能有扰流板)将不胜感激。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句