我正在尝试编写一个名为Sender.java的java类来调用servlet。 这是Sender.java的代码:
public class Sender {
public static void main(String args[]){
try{
URL gwtServlet = new URL("http://localhost/FileReceiver/Receiver");
HttpURLConnection servletConnection = (HttpURLConnection) gwtServlet.openConnection();
servletConnection.setRequestMethod("POST");
servletConnection.setDoOutput(true);
servletConnection.setDoInput(true);
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches (false);
OutputStream os = servletConnection.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
out.writeInt(1);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
这里是servlet Receiver.java的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
InputStream is = request.getInputStream();
//until here is ok
ObjectInputStream ois = new ObjectInputStream(is);
int p = ois.readInt();
writer.println("the number p is : " + p);
ois.close();
} catch (Exception exc) {
exc.printStackTrace();
}
}
当我尝试在服务器(WildFly 8.x)上运行servlet时,我有这个错误:
14:52:20,193 ERROR [stderr] (default task-7) java.io.EOFException
14:52:20,193 ERROR [stderr] (default task-7) at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
14:52:20,193 ERROR [stderr] (default task-7) at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
14:52:20,193 ERROR [stderr] (default task-7) at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
14:52:20,193 ERROR [stderr] (default task-7) at java.io.ObjectInputStream.<init>(Unknown Source)
14:52:20,193 ERROR [stderr] (default task-7) at fileServlet.servlet.Receiver.doPost(Receiver.java:67)
14:52:20,193 ERROR [stderr] (default task-7) at fileServlet.servlet.Receiver.doGet(Receiver.java:46)
14:52:20,193 ERROR [stderr] (default task-7) at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
14:52:20,193 ERROR [stderr] (default task-7) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
14:52:20,193 ERROR [stderr] (default task-7) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
14:52:20,193 ERROR [stderr] (default task-7) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
14:52:20,193 ERROR [stderr] (default task-7) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
14:52:20,194 ERROR [stderr] (default task-7) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
14:52:20,194 ERROR [stderr] (default task-7) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
14:52:20,194 ERROR [stderr] (default task-7) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
14:52:20,194 ERROR [stderr] (default task-7) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
14:52:20,194 ERROR [stderr] (default task-7) at java.lang.Thread.run(Unknown Source)
我知道问题是关于InputStream is = request.getInputStream();
,因为我可以从调试器看到这个( 是空的),但我不知道为什么会这样。
有人能帮助我吗?提前谢谢你。
答案 0 :(得分:1)
如果要从Java客户端到Servlet执行HTTP POST
,则必须满足以下要求 :
Content-Type
标题必须为application/x-www-form-urlencoded
。这通常是默认值。如果您要发送文件数据或任何相关数据流,请将Content-Type
设置为multipart/form-data
。这要求发送的信息符合RFC2388。OutputStream
发送到Servlet的值必须是编码的(application/x-www-form-urlencoded
)键/值对(由&
分隔)。Content-Length
标题必须是2中提到的编码值的长度,或者是您愿意发送给服务器/ servlet的所有数据的长度。< / LI>
醇>
在Servlet端,您可以使用request.getParameter()
方法(传递参数键)来获取其值。对于multipart/form-data
,您可以使用request.getInputStream()
。
我希望这会有所帮助。一个很好的例子,由BalusC在StackOverflow上编写,关于如何用Java激活HTTP POST,可以找到here。