Java Jetty:提供消息正文总是导致状态_除外
我们使用Jetty将数据从服务器应用程序发送到客户机应用程序,也使用Jetty 由于某些原因,当设置消息内容时,响应始终为HttpExchange.STATUS_(例外)。然而,消息在另一端被接收,一切似乎都完好无损 这是正在生成和发送请求的服务器应用程序的代码:Java Jetty:提供消息正文总是导致状态_除外,java,http,jetty,Java,Http,Jetty,我们使用Jetty将数据从服务器应用程序发送到客户机应用程序,也使用Jetty 由于某些原因,当设置消息内容时,响应始终为HttpExchange.STATUS_(例外)。然而,消息在另一端被接收,一切似乎都完好无损 这是正在生成和发送请求的服务器应用程序的代码: HttpClient client = new HttpClient(); try { client.start(); ContentExchange
HttpClient client = new HttpClient();
try
{
client.start();
ContentExchange exchange = new ContentExchange(true);
//exchange.setMethod("POST");
// Set the request URL
logger.info("1");
exchange.setURL("http://localhost:8180/network/");
// Set the data contents type.
logger.info("2");
//exchange.setRequestContentType("application/zip");
// Create a file input stream
logger.info("3");
FileInputStream inputFile = new FileInputStream(new File("ECF43d01.zip"));
// Stream the file into the request
logger.info("4");
exchange.setRequestContentSource(inputFile); // <- This line makes the response always STATUS_EXCEPTED
// Send the request
logger.info("5");
client.send(exchange);
//logger.info(baseRequest.getMethod());
// Waits until the exchange is terminated
int exchangeState = exchange.waitForDone();
if (exchangeState == HttpExchange.STATUS_COMPLETED)
response.getWriter().println("Completed "+exchange.getResponseContent());
else if (exchangeState == HttpExchange.STATUS_EXCEPTED)
response.getWriter().println("Error with handler HttpExchange.STATUS_EXCEPTED");
else if (exchangeState == HttpExchange.STATUS_EXPIRED)
response.getWriter().println("Timeout");
您不能简单地将二进制字节从ZIP文件传输到HTTP请求中。您可能会考虑UUTCODE并确保该方法是POST,因此它不会被绑定到请求URL的末尾。下面是一个很好的例子:
从我所做的工作来看,这样的数据打包处理是正常的。数据从消息体的另一端出来。尽管我可能应该在问题描述中加入这一点。
public void handle(String target, Request baseRequest, HttpServletRequest arg2, HttpServletResponse response) throws IOException, ServletException
{
Logger logger = LoggerFactory.getLogger(AbstractHandler.class);
logger.info("Starting");
response.setContentType("text/html;charset=utf-8");
logger.info("Output");
// We have handled the request. Send back a successful response
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
}