Libraryless. Click here for Pure Java version (4745L/35K/96K).
1 | !752 |
2 | |
3 | static S dir = "/storage/sdcard0"; |
4 | |
5 | p { |
6 | serveHttp(7777); |
7 | run("#1000842"); // upload server on 8888 |
8 | } |
9 | |
10 | static NanoHTTPD.Response serve(S uri, NanoHTTPD.Method method, |
11 | Map<S,S> header, Map<S,S> params, Map<S,S> files) { |
12 | print("Serving HTML."); |
13 | |
14 | File d = new File(dir); |
15 | File sub = new File(dir, dropPrefix("/", uri)); |
16 | print("File: " + sub.getAbsolutePath() + ", exists: " + sub.exists()); |
17 | |
18 | S html; |
19 | |
20 | if (!sub.exists()) |
21 | html = "Not found: " + quote(f2s(sub)) + " (uri: " + quote(uri) + ")"; |
22 | else if (sub.isFile()) |
23 | ret serveFile(sub); |
24 | else |
25 | html = fileLinks(findAllFiles(sub), d); |
26 | |
27 | ret serveHTML(htitle("Phone Server.") |
28 | + p(href("http://" + getMyIP() + ":8888", "Upload stuff")) |
29 | + h3(sub.getAbsolutePath()) + html); |
30 | } |
31 | |
32 | static S fileLinks(L<File> files, File rootDir) { |
33 | new L<S> l; |
34 | for (File f : files) { |
35 | S path = getRelPath(f, rootDir); |
36 | l.add(href(urlencode(dropPrefix("/", path)), path)); |
37 | } |
38 | ret ul(l); |
39 | } |
40 | |
41 | static S getRelPath(File f, File root) ctex { |
42 | ret dropPrefix(root.getCanonicalPath(), f.getCanonicalPath()); |
43 | } |
Began life as a copy of #1001649
download show line numbers debug dex old transpilations
Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, crvwmplrxojx, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pnmttuucjkfb, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002646 |
Snippet name: | "Phone Server" - Serve local directory (HTTP, Android, unprotected, WORKS) |
Eternal ID of this version: | #1002646/1 |
Text MD5: | 43465667c5bf32b99054ea9438c26d1e |
Transpilation MD5: | 51fe98f7da18bcb947b85cddb8afde46 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-12-13 01:00:53 |
Source code size: | 1125 bytes / 43 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 884 / 1070 |
Referenced in: | [show references] |