Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

43
LINES

< > TinyBrain | #1002646 - "Phone Server" - Serve local directory (HTTP, Android, unprotected, WORKS)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4745L/35K/96K).

!752

static S dir = "/storage/sdcard0";

p {
  serveHttp(7777);
  run("#1000842"); // upload server on 8888
}

static NanoHTTPD.Response serve(S uri, NanoHTTPD.Method method,
  Map<S,S> header, Map<S,S> params, Map<S,S> files) {
  print("Serving HTML.");
  
  File d = new File(dir);
  File sub = new File(dir, dropPrefix("/", uri));
  print("File: " + sub.getAbsolutePath() + ", exists: " + sub.exists());
  
  S html;
  
  if (!sub.exists())
    html = "Not found: " + quote(f2s(sub)) + " (uri: " + quote(uri) + ")";
  else if (sub.isFile())
    ret serveFile(sub);
  else
    html = fileLinks(findAllFiles(sub), d);

  ret serveHTML(htitle("Phone Server.")
    + p(href("http://" + getMyIP() + ":8888", "Upload stuff"))
    + h3(sub.getAbsolutePath()) + html);
}

static S fileLinks(L<File> files, File rootDir) {
  new L<S> l;
  for (File f : files) {
    S path = getRelPath(f, rootDir);
    l.add(href(urlencode(dropPrefix("/", path)), path));
  }
  ret ul(l);
}

static S getRelPath(File f, File root) ctex {
  ret dropPrefix(root.getCanonicalPath(), f.getCanonicalPath());
}

Author comment

Began life as a copy of #1001649

download  show line numbers  debug dex   

Travelled to 8 computer(s): cfunsshuasjs, crvwmplrxojx, gwrvuhgaqvyk, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pnmttuucjkfb, tvejysmllsmz

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: 298 / 378
Referenced in: [show]