本文整理汇总了Java中com.sun.net.httpserver.HttpExchange.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java HttpExchange.setAttribute方法的具体用法?Java HttpExchange.setAttribute怎么用?Java HttpExchange.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.net.httpserver.HttpExchange
的用法示例。
在下文中一共展示了HttpExchange.setAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void handle(final HttpExchange paramHttpExchange) throws IOException {
String path = paramHttpExchange.getRequestURI().getPath();
if (path.startsWith("/")) {
path = path.substring(1);
}
final String [] swp = path.split("/");
final GUIOperations operation = createGUIOperation(swp);
if (operation.equals(GUIOperations.EVENT_POLL)) {
final String response = callEventPoll();
paramHttpExchange.sendResponseHeaders(200, 0);
paramHttpExchange.setAttribute("Content-Length", Integer.valueOf(response.length()));
paramHttpExchange.getResponseBody().write(response.getBytes());
paramHttpExchange.close();
} else {
final String json = readRequestBody(paramHttpExchange.getRequestBody());
handle(operation, swp.length > 1 ? swp[1] : null, json);
paramHttpExchange.sendResponseHeaders(200, 0);
paramHttpExchange.close();
}
}
示例2: doFilter
import com.sun.net.httpserver.HttpExchange; //导入方法依赖的package包/类
@Override
public void doFilter(HttpExchange exchange, Chain chain) throws IOException
{
final Map<String, List<String>> params = parseGetParameters(exchange);
Map<String, Pair<String, File>> streams = null;
if( HttpExchangeUtils.isPost(exchange) )
{
if( HttpExchangeUtils.isMultipartContent(exchange) )
{
streams = new HashMap<String, Pair<String, File>>();
try
{
FileItemIterator ii = new FileUpload().getItemIterator(new ExchangeRequestContext(exchange));
while( ii.hasNext() )
{
final FileItemStream is = ii.next();
final String name = is.getFieldName();
try( InputStream stream = is.openStream() )
{
if( !is.isFormField() )
{
// IE passes through the full path of the file,
// where as Firefox only passes through the
// filename. We only need the filename, so
// ensure that we string off anything that looks
// like a path.
final String filename = STRIP_PATH_FROM_FILENAME.matcher(is.getName()).replaceFirst(
"$1");
final File tempfile = File.createTempFile("equella-manager-upload", "tmp");
tempfile.getParentFile().mkdirs();
streams.put(name, new Pair<String, File>(filename, tempfile));
try( OutputStream out = new BufferedOutputStream(new FileOutputStream(tempfile)) )
{
ByteStreams.copy(stream, out);
}
}
else
{
addParam(params, name, Streams.asString(stream));
}
}
}
}
catch( Exception t )
{
throw new RuntimeException(t);
}
}
else
{
try( InputStreamReader isr = new InputStreamReader(exchange.getRequestBody(), "UTF-8") )
{
BufferedReader br = new BufferedReader(isr);
String query = br.readLine();
parseQuery(query, params);
}
}
}
exchange.setAttribute(PARAMS_KEY, params);
exchange.setAttribute(MULTIPART_STREAMS_KEY, streams);
// attributes seem to last the life of a session... I don't know why...
exchange.setAttribute("error", null);
chain.doFilter(exchange);
}