@Bean public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean result = new ServletRegistrationBean(new CGIServlet(), this.cgiPathMapping); result.addInitParameter("executable", ""); result.addInitParameter("cgiPathPrefix", this.cgiPathPrefix); result.addInitParameter("parameterEncoding", StandardCharsets.UTF_8.displayName()); result.addInitParameter("passShellEnvironment", "true"); return result; }
public static void upload( UUID uuid, String file, String extension, RunnableVal<OutputStream> writeTask, RunnableVal<URL> whenDone) { if (writeTask == null) { PS.debug("&cWrite task cannot be null"); TaskManager.runTask(whenDone); return; } String filename; String website; if (uuid == null) { uuid = UUID.randomUUID(); website = Settings.Web.URL + "upload.php?" + uuid; filename = "plot." + extension; } else { website = Settings.Web.URL + "save.php?" + uuid; filename = file + '.' + extension; } URL url; try { url = new URL( Settings.Web.URL + "?key=" + uuid + "&ip=" + Settings.Web.SERVER_IP + "&type=" + extension); } catch (MalformedURLException e) { e.printStackTrace(); whenDone.run(); return; } TaskManager.runTaskAsync( () -> { try { String boundary = Long.toHexString(System.currentTimeMillis()); URLConnection con = new URL(website).openConnection(); con.setDoOutput(true); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); try (OutputStream output = con.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8), true)) { String CRLF = "\r\n"; writer.append("--").append(boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF); writer .append("Content-Type: text/plain; charset=") .append(StandardCharsets.UTF_8.displayName()) .append(CRLF); String param = "value"; writer.append(CRLF).append(param).append(CRLF).flush(); writer.append("--").append(boundary).append(CRLF); writer .append("Content-Disposition: form-data; name=\"schematicFile\"; filename=\"") .append(filename) .append('"') .append(CRLF); writer .append("Content-Type: ") .append(URLConnection.guessContentTypeFromName(filename)) .append(CRLF); writer.append("Content-Transfer-Encoding: binary").append(CRLF); writer.append(CRLF).flush(); writeTask.value = output; writeTask.run(); output.flush(); writer.append(CRLF).flush(); writer.append("--").append(boundary).append("--").append(CRLF).flush(); } // try (Reader response = new InputStreamReader(con.getInputStream(), // StandardCharsets.UTF_8)) { // final char[] buffer = new char[256]; // final StringBuilder result = new StringBuilder(); // while (true) { // final int r = response.read(buffer); // if (r < 0) { // break; // } // result.append(buffer, 0, r); // } // if (!result.toString().startsWith("Success")) { // PS.debug(result); // } // } catch (IOException e) { // e.printStackTrace(); // } int responseCode = ((HttpURLConnection) con).getResponseCode(); if (responseCode == 200) { whenDone.value = url; } TaskManager.runTask(whenDone); } catch (IOException e) { e.printStackTrace(); TaskManager.runTask(whenDone); } }); }