!752 static int port = 8888; static MyHTTPD server; static Object onClient; // should implement GF // Generic function interface static interface GF { Object run(Object... args); } psvm { start(); sleep(); } static void start() ctex { // Android: Make NanoHTTPD store temp files on the right drive if (isAndroid()) { S dir = getProgramDir().getAbsolutePath(); System.setProperty("java.io.tmpdir", dir); print("Using temp dir " + dir); } server = new MyHTTPD(port); server.start(); System.out.println("HTTP server started (port " + port + ", file upload)"); printMyIPs(); } static File saveUploadedFile(File tmpfile, String originalName) ctex { File file = new File(userHome(), "JavaX-Uploads/" + sanitizeFileName(originalName)); mkdirsForFile(file); /*if (!tmpfile.renameTo(file)) fail("Cannot rename " + tmpfile.getAbsolutePath() + " to " + file.getAbsolutePath());*/ print("File length: " + tmpfile.length()); copyFile(tmpfile, file); return file; } static String sanitizeFileName(String s) { return s.replaceAll("[^a-zA-Z0-9\\-_\\. ]", ""); } static class MyHTTPD extends NanoHTTPD { public MyHTTPD(int port) throws IOException { super(port); } public Response serve(final String uri, final Method method, final Map header, final Map parms, final Map files ) { try { thread { if (onClient != null) call(onClient, "run", new Object[] {new Object[] {uri, method, header, parms, files}}); } System.out.println( method + " '" + uri + "' " ); if (uri.equals("/upload")) { String tmpfile = files.get("file"); String originalName = parms.get("file"); bool test = nempty(parms.get("test")); S msg; if (test) msg = originalName + " would be uploaded from " + tmpfile; else { File file = main.saveUploadedFile(new File(tmpfile), originalName); msg = originalName + " uploaded as " + file.getAbsolutePath() + ", thank you."; } print(msg); return newFixedLengthResponse(msg); } else { String msg = "

Upload server

\n"; msg += [[


Test mode
]]; msg += "\n"; return newFixedLengthResponse(msg); } } catch (Throwable e) { e.printStackTrace(); return newFixedLengthResponse(e.toString()); } } }