本文整理匯總了Java中fi.iki.elonen.NanoHTTPD.newFixedLengthResponse方法的典型用法代碼示例。如果您正苦於以下問題:Java NanoHTTPD.newFixedLengthResponse方法的具體用法?Java NanoHTTPD.newFixedLengthResponse怎麽用?Java NanoHTTPD.newFixedLengthResponse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fi.iki.elonen.NanoHTTPD
的用法示例。
在下文中一共展示了NanoHTTPD.newFixedLengthResponse方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doGet
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
public Response doGet(IHTTPSession session) {
String uri = session.getUri();
String path = uri.substring(getRequestMappingURL().length());
File result = new File(basePath, path);
String requestCanonicalPath;
try {
requestCanonicalPath = result.getCanonicalPath();
} catch (IOException e1) {
return new503ErrorResponse();
}
//prevent escape from configured base directory
if (!requestCanonicalPath.startsWith(canonicalBasePath) || !result.exists()) {
return NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "not found");
}
try {
return NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.Response.Status.OK, getMimeType(uri), new FileInputStream(result), result.length());
} catch (FileNotFoundException e) {
return new503ErrorResponse();
}
}
示例2: get
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
@Override
public Response get(UriResource uriResource, Map<String, String> urlParams, IHTTPSession session) {
Method method = session.getMethod();
String uri = session.getUri();
//Log.d(TAG, "Method: " + method + ", Url: " + uri);
try {
EpubFetcher fetcher = uriResource.initParameter(EpubFetcher.class);
int offset = uri.indexOf("/", 0);
int startIndex = uri.indexOf("/", offset + 1);
String filePath = uri.substring(startIndex + 1);
Link link = fetcher.publication.getResourceMimeType(filePath);
String mimeType = link.getTypeLink();
InputStream inputStream = fetcher.getDataInputStream(filePath);
response = serveResponse(session, inputStream, mimeType);
} catch (EpubFetcherException e) {
e.printStackTrace();
return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, getMimeType(), ResponseStatus.FAILURE_RESPONSE);
}
return response;
}
示例3: get
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
String sessionId = urlParams.get("sessionId");
String elementId = urlParams.get("elementId");
try {
Element el = Elements.getGlobal().getElement(elementId);
final Rect rect = el.element.getVisibleBounds();
JSONObject res = new JSONObject();
res.put("x", rect.left);
res.put("y", rect.top);
res.put("height", rect.height());
res.put("width", rect.width());
return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(res, sessionId).toString());
} catch (final Exception e) {
return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.UnknownError, sessionId).toString());
}
}
示例4: serveOrCatch
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
public NanoHTTPD.Response serveOrCatch(NanoHttpdServer server, NanoHTTPD.IHTTPSession session) throws Exception {
switch (session.getMethod()) {
case GET:
return get.serve(server, session);
case PUT:
return put.serve(server, session);
default:
NanoMaven.getLogger().error("Unknown method: " + session.getUri());
return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND, NanoHTTPD.MIME_HTML, "Unknown method");
}
}
示例5: serve
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
private Response serve(IHTTPSession session) {
String path = session.getUri();
Map<String, String> parameters = session.getParms();
LOGGER.debug("Request received: {} {}", path, parameters);
String resource = resources.getResource(path, parameters);
if (resource == null) {
resource = resources.getResource("404");
}
if (resource == null) {
resource = "Not found.";
}
return NanoHTTPD.newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_PLAINTEXT, resource);
}
示例6: process
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
public Response process(Map<String, String> urlParams, IHTTPSession session) {
String error = "General error!";
if (handlerObject != null || handler != null) {
try {
Object object = ((handlerObject != null) ? handlerObject : handler.newInstance());
if (object instanceof UriResponder) {
UriResponder responder = (UriResponder) object;
switch (this.method) {
case Methods.GET:
return responder.get(this, urlParams, session);
case Methods.POST:
return responder.post(this, urlParams, session);
case Methods.PUT:
return responder.put(this, urlParams, session);
case Methods.DELETE:
return responder.delete(this, urlParams, session);
default:
return responder.other(session.getMethod().toString(), this, urlParams, session);
}
} else {
return NanoHTTPD.newFixedLengthResponse(Status.OK, "text/plain", //
new StringBuilder("Return: ")//
.append(handler.getCanonicalName())//
.append(".toString() -> ")//
.append(object)//
.toString());
}
} catch (Exception e) {
error = "Error: " + e.getClass().getName() + " : " + e.getMessage();
LOG.log(Level.SEVERE, error, e);
}
}
return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", error);
}
示例7: serve
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
@Override
public Response serve(IHTTPSession session) {
String uri = session.getUri();
for (Map.Entry<String, String> route : routes.entrySet()) {
if (!uri.contains(route.getKey())) {
continue;
}
return NanoHTTPD.newFixedLengthResponse(route.getValue());
}
return super.serve(session);
}
示例8: get
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
String sessionId = urlParams.get("sessionId");
return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
示例9: get
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
@Override
public NanoHTTPD.Response get(RouterNanoHTTPD.UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) {
String sessionId = urlParams.get("sessionId");
return NanoHTTPD.newFixedLengthResponse(getStatus(), getMimeType(), new Response(Status.NoSuchElement, sessionId).toString());
}
示例10: createResponse
import fi.iki.elonen.NanoHTTPD; //導入方法依賴的package包/類
private Response createResponse(Status status, String mimeType, String message) {
Response response = NanoHTTPD.newFixedLengthResponse(status, mimeType, message);
response.addHeader("Accept-Ranges", "bytes");
return response;
}