当前位置: 首页>>代码示例>>Java>>正文


Java HttpExchange.setAttribute方法代码示例

本文整理汇总了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();
	}
}
 
开发者ID:gamefest2017,项目名称:ants,代码行数:25,代码来源:RequestHandler.java

示例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);
}
 
开发者ID:equella,项目名称:Equella,代码行数:71,代码来源:SuperDuperFilter.java


注:本文中的com.sun.net.httpserver.HttpExchange.setAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。