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).

1  
!752
2  
3  
static int port = 8888;
4  
5  
static MyHTTPD server;
6  
static Object onClient; // should implement GF
7  
8  
// Generic function interface
9  
static interface GF {
10  
  Object run(Object... args);
11  
}
12  
13  
psvm {
14  
  start();
15  
  sleep();
16  
}
17  
18  
static void start() ctex {
19  
  // Android: Make NanoHTTPD store temp files on the right drive
20  
  if (isAndroid()) {
21  
    S dir = getProgramDir().getAbsolutePath();
22  
    System.setProperty("java.io.tmpdir", dir);
23  
    print("Using temp dir " + dir);
24  
  }
25  
  
26  
  server = new MyHTTPD(port);
27  
  server.start();
28  
  System.out.println("HTTP server started (port " + port + ", file upload)");
29  
  printMyIPs();
30  
}
31  
32  
static File saveUploadedFile(File tmpfile, String originalName) ctex {
33  
  File file = new File(userHome(), "JavaX-Uploads/" + sanitizeFileName(originalName));
34  
  mkdirsForFile(file);
35  
  /*if (!tmpfile.renameTo(file))
36  
    fail("Cannot rename " + tmpfile.getAbsolutePath() + " to " + file.getAbsolutePath());*/
37  
  print("File length: " + tmpfile.length());
38  
  copyFile(tmpfile, file);
39  
  return file;
40  
}
41  
42  
static String sanitizeFileName(String s) {
43  
  return s.replaceAll("[^a-zA-Z0-9\\-_\\. ]", "");
44  
}
45  
46  
static class MyHTTPD extends NanoHTTPD {
47  
48  
  public MyHTTPD(int port) throws IOException {
49  
      super(port);
50  
  }
51  
52  
  public Response serve(final String uri, final Method method,
53  
          final Map<String, String> header, final Map<String, String> parms,
54  
          final Map<String, String> files )
55  
  {
56  
      try {
57  
      
58  
      thread {
59  
        if (onClient != null)
60  
          call(onClient, "run", new Object[] {new Object[] {uri, method, header, parms, files}});
61  
      }
62  
      
63  
      System.out.println( method + " '" + uri + "' " );
64  
      
65  
      if (uri.equals("/upload")) {
66  
        String tmpfile = files.get("file");
67  
        String originalName = parms.get("file");
68  
        bool test = nempty(parms.get("test"));
69  
        
70  
        S msg;
71  
        if (test)
72  
          msg = originalName + " would be uploaded from " + tmpfile;
73  
        else {
74  
          File file = main.saveUploadedFile(new File(tmpfile), originalName);
75  
76  
          msg = originalName + " uploaded as " + file.getAbsolutePath() + ", thank you.";
77  
        }
78  
        
79  
        print(msg);
80  
        return newFixedLengthResponse(msg);
81  
        
82  
      } else {
83  
        String msg = "<html><body><h1>Upload server</h1>\n";
84  
       
85  
        msg += [[ 
86  
<form method="post" enctype="multipart/form-data" action="upload">
87  
    <input type="file" name="file" />
88  
    <br>
89  
    <input type="submit" value="Send" />
90  
    <br>
91  
    <input type="checkbox" name="test" />Test mode
92  
    <br>
93  
</form>
94  
]];
95  
96  
        msg += "</body></html>\n";
97  
        return newFixedLengthResponse(msg);
98  
      }
99  
    } catch (Throwable e) {
100  
      e.printStackTrace();
101  
      return newFixedLengthResponse(e.toString());
102  
    }
103  
  }
104  
}

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: 948 / 1706
Referenced in: [show references]