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

104
LINES

< > BotCompany Repo | #1000842 // HTTP file upload server (works)

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (2956L/23K/59K).

!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<String, String> header, final Map<String, String> parms,
          final Map<String, String> 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 = "<html><body><h1>Upload server</h1>\n";
       
        msg += [[ 
<form method="post" enctype="multipart/form-data" action="upload">
    <input type="file" name="file" />
    <br>
    <input type="submit" value="Send" />
    <br>
    <input type="checkbox" name="test" />Test mode
    <br>
</form>
]];

        msg += "</body></html>\n";
        return newFixedLengthResponse(msg);
      }
    } catch (Throwable e) {
      e.printStackTrace();
      return newFixedLengthResponse(e.toString());
    }
  }
}

Author comment

Began life as a copy of #1000436

download  show line numbers  debug dex  old transpilations   

Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, crvwmplrxojx, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pnmttuucjkfb, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000842
Snippet name: HTTP file upload server (works)
Eternal ID of this version: #1000842/1
Text MD5: e57b59f93543a079721305943dc8c6fe
Transpilation MD5: b912565c30fabfba0af368069f3f9a76
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-17 22:37:14
Source code size: 2860 bytes / 104 lines
Pitched / IR pitched: No / No
Views / Downloads: 945 / 1701
Referenced in: [show references]