Skip to content

Instantly share code, notes, and snippets.

@frankkienl
Created August 11, 2013 15:57
Show Gist options
  • Save frankkienl/6205463 to your computer and use it in GitHub Desktop.
Save frankkienl/6205463 to your computer and use it in GitHub Desktop.
Upload script for simple WebServer
package nl.frankkie.web;
import java.io.File;
import java.util.Map;
import fi.iki.elonen.NanoHTTPD;
/**
* Created by FrankkieNL on 11-8-13.
*/
public class Upload extends WebPage {
public static final String page = "<html>" +
"<head><title>Web</title></head>" +
"<body>" +
"<h2>Web</h2>" +
"<a href=\"/\">HOME</a>" +
"<form enctype=\"multipart/form-data\" action=\"/upload\" method=\"post\">\n" +
"<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"2000000\">\n" +
"File: <input name=\"uploadFile\" type=\"file\"><br>\n" +
"Path: <input type=\"text\" name=\"path\" value=\"/sdcard/uploads/\"><br>\n" +
"<input name=\"gezien\" value=\"ja\" type=\"hidden\">\n" +
"<input type=\"submit\" value=\"Start Upload\" name=\"submitButton\">\n" +
"</form>" +
"</body>" +
"</html>";
@Override
public NanoHTTPD.Response serve(String uri, NanoHTTPD.Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) {
NanoHTTPD.Response response = null;
if (method.equals(NanoHTTPD.Method.POST)) {
String tempFilename = files.get("uploadFile");
if (tempFilename != null) {
//rename
File file = new File(tempFilename);
try {
//
File path = new File(parms.get("path"));
path.mkdirs();
//
file.renameTo(new File(parms.get("path") + parms.get("uploadFile")));
response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_HTML, "OK !");
} catch (Exception e) {
response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_HTML, "Upload Error !");
}
} else {
response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_HTML, "Upload Error !");
}
return response;
}
///
StringBuilder sb = new StringBuilder();
sb.append(page);
response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_HTML, sb.toString());
return response;
}
}
@ItsHarper
Copy link

ItsHarper commented Nov 30, 2018

Sorry for commenting on something so old, but what is the WebPage class from? Is its source available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment