Created
November 28, 2023 23:26
-
-
Save chop0/f498725d35773161a5ad0d6884c336fc to your computer and use it in GitHub Desktop.
simple java file upload server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/java --source 21 | |
import java.io.*; | |
import java.net.*; | |
import java.nio.file.*; | |
import com.sun.net.httpserver.*; | |
public class FileUploadServer implements HttpHandler { | |
public static void main(String[] args) throws IOException { | |
if (args.length != 2) { | |
System.out.printf("Usage: %s <port> <root>%n", FileUploadServer.class.getName()); | |
System.exit(1); | |
} | |
var root = Path.of(args[1]); | |
var port = Integer.parseInt(args[0]); | |
if (!Files.isDirectory(root)) { | |
System.out.printf("Root %s is not a directory%n", root); | |
System.exit(1); | |
} | |
var server = HttpServer.create(); | |
server.createContext("/", new FileUploadServer(root)); | |
server.bind(new InetSocketAddress(port), 0); | |
server.start(); | |
} | |
private final Path root; | |
private final HttpHandler delegate; | |
public FileUploadServer(Path root) { | |
this.root = root; | |
this.delegate = SimpleFileServer.createFileHandler(root.toAbsolutePath()); | |
} | |
private void handlePut(HttpExchange exchange) throws IOException { | |
try (exchange) { | |
URI accessibleURL; | |
Path destination; | |
if (exchange.getRequestURI().getPath().equals("/") || exchange.getRequestURI().getPath().isEmpty()) { | |
var filename = randomAlphaNumeric(10); | |
destination = root.resolve(filename); | |
accessibleURL = exchange.getRequestURI().resolve(filename); | |
} else { | |
destination = root.resolve(exchange.getRequestURI().getPath()); | |
accessibleURL = exchange.getRequestURI(); | |
} | |
Files.createDirectories(destination.getParent()); | |
Files.write(destination, exchange.getRequestBody().readAllBytes()); | |
System.out.println(accessibleURL); | |
if (accessibleURL.getHost() == null) | |
accessibleURL = new URI("http", null, exchange.getLocalAddress().getHostName(), exchange.getLocalAddress().getPort(), accessibleURL.getPath(), null, null); | |
exchange.sendResponseHeaders(201, accessibleURL.toString().length()); | |
exchange.getResponseBody().write(accessibleURL.toString().getBytes()); | |
} catch (URISyntaxException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
public void handle(HttpExchange exchange) throws IOException { | |
switch (exchange.getRequestMethod()) { | |
case "GET" -> delegate.handle(exchange); | |
case "PUT" -> handlePut(exchange); | |
} | |
} | |
private static String randomAlphaNumeric(int count) { | |
var builder = new StringBuilder(); | |
for (int i = 0; i < count; i++) { | |
builder.append((char) (Math.random() * 26 + 'a')); | |
} | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment