httpExchange.getRequestBody()获得的是什么数据,这个函数的作用是什么?可以举个实例吗

schnuffelrr 2015-09-15 02:39:08


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.spi.HttpServerProvider;

public class ViewServer {
//启动服务,监听来自客户端的请求
public static void httpserverService() throws IOException {
HttpServerProvider provider = HttpServerProvider.provider();
HttpServer httpserver =provider.createHttpServer(new InetSocketAddress(6666), 100);//监听端口6666,能同时接 受100个请求
httpserver.createContext("/myApp", new MyHttpHandler()); //static 静态才可以调用
httpserver.setExecutor(null);
httpserver.start();
System.out.println("server started");
}
//Http请求处理类
static class MyHttpHandler implements HttpHandler { //static静态
public void handle(HttpExchange httpExchange) throws IOException {
String responseMsg = "okl"; //响应信息
//
InputStream in = new FileInputStream("c:\\jack2.txt");
//InputStream in = httpExchange.getRequestBody(); //获得输入流 the stream from which the request body can be read.

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String temp = null;
while((temp = reader.readLine()) != null) {
System.out.println("client request:"+temp);
}

httpExchange.sendResponseHeaders(200, responseMsg.length()); //设置响应头属性及响应信息的长度
OutputStream out = httpExchange.getResponseBody(); //获得输出流
out.write(responseMsg.getBytes());
out.flush();
httpExchange.close();

}
}
public static void main(String[] args) throws IOException {
httpserverService();
}
}
...全文
295 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
兔子托尼啊 2015-09-15
获得输入流 。。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-09-15 02:39
社区公告
暂无公告