java 读取发布请求值 HttpHandler
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3409348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
read post request values HttpHandler
提问by Haim Bender
I'm writing a little Java app which implements an http service that receives http post commands from a client.
我正在编写一个小的 Java 应用程序,它实现了一个从客户端接收 http post 命令的 http 服务。
The class I'm using to implement all of this is HttpHandler and HttpServer in the com.sun.net. package.
我用来实现所有这些的类是 com.sun.net 中的 HttpHandler 和 HttpServer。包裹。
Now I'm implementing an handle(HttpExchange exchange) function which handles the request, and I'm having truble reading the post values received by the request because the only access that I have to these values is via HttpExchange.getResponseBody() which is just an output stream.
现在我正在实现一个处理请求的 handle(HttpExchange exchange) 函数,并且我正在读取请求收到的 post 值,因为我对这些值的唯一访问是通过 HttpExchange.getResponseBody() 这是只是一个输出流。
I'm looking to parse txt post values and uploaded files.
我正在寻找解析 txt 帖子值和上传的文件。
Please help.
请帮忙。
Thanks.
谢谢。
回答by Maurice Perry
I have written classes that process multipart requests for my project Sceye-Fi, an HTTP server that uses the com.sun.net.httpserverclasses that come with java 6, to receive photo uploads from an Eye-Ficard.
我编写了一些类来处理我的项目Sceye-Fi 的多部分请求,Sceye-Fi是一个 HTTP 服务器,它使用com.sun.net.httpserverJava 6 附带的类来接收来自Eye-Fi卡的照片上传。
This can help with file uploads (multipart posts).
这有助于文件上传(多部分帖子)。
For a non-multipart post, you would need to do something like this:
对于非多部分帖子,您需要执行以下操作:
// determine encoding
Headers reqHeaders = exchange.getRequestHeaders();
String contentType = reqHeaders.getFirst("Content-Type");
String encoding = "ISO-8859-1";
if (contentType != null) {
Map<String,String> parms = ValueParser.parse(contentType);
if (parms.containsKey("charset")) {
encoding = parms.get("charset");
}
}
// read the query string from the request body
String qry;
InputStream in = exchange.getRequestBody();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte buf[] = new byte[4096];
for (int n = in.read(buf); n > 0; n = in.read(buf)) {
out.write(buf, 0, n);
}
qry = new String(out.toByteArray(), encoding);
} finally {
in.close();
}
// parse the query
Map<String,List<String>> parms = new HashMap<String,List<String>>();
String defs[] = qry.split("[&]");
for (String def: defs) {
int ix = def.indexOf('=');
String name;
String value;
if (ix < 0) {
name = URLDecoder.decode(def, encoding);
value = "";
} else {
name = URLDecoder.decode(def.substring(0, ix), encoding);
value = URLDecoder.decode(def.substring(ix+1), encoding);
}
List<String> list = parms.get(name);
if (list == null) {
list = new ArrayList<String>();
parms.put(name, list);
}
list.add(value);
}
回答by fglez
An alternative would be using HttpServicefrom HttpCore.
另一种方法是使用HttpService的距离的HttpCore。
There is a Basic HTTP server example in the documentation
文档中有一个基本的 HTTP 服务器示例

