!752 p { S ip = first(detectGateways()); assertNotNull(ip); bool test = true; S url = "http://" + ip + ":8888/upload"; print("Uploading to: " + url); new Map params; if (test) params.put("test", "1"); String charset = "UTF-8"; File binaryFile = loadLibrary("#1000206"); String boundary = randomID(20); // Just generate some unique random value. String CRLF = "\r\n"; // Line separator required by multipart/form-data. URLConnection connection = new URL(url).openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream output = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // Send normal param. for (S param : keys(params)) { writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"" + urlencode(param) + "\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // TODO: encode?? writer.append(CRLF).append(urlencode(params.get(param))).append(CRLF); } // Send binary file. writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF); writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF); writer.append("Content-Transfer-Encoding: binary").append(CRLF); writer.append(CRLF).flush(); file2stream(binaryFile, output); output.flush(); // Important before continuing with writer! writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary. // End of multipart/form-data. writer.append("--" + boundary + "--").append(CRLF).flush(); writer.close(); output.close(); print("Sent."); print(); print(loadPage(connection, new URL(url))); }