static void uploadFileToPhoneServer(File binaryFile) ctex { if (binaryFile == null || !binaryFile.isFile()) ret; S ip = first(detectGateways()); assertNotNull(ip); S url = "http://" + ip + ":8888/upload"; print("Uploading to: " + url); new Map<S, S> params; String charset = "UTF-8"; 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. (TODO: I don't think this works) 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))); }
Began life as a copy of #1003776
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1003778 |
Snippet name: | uploadFileToPhoneServer |
Eternal ID of this version: | #1003778/1 |
Text MD5: | f701b92e796c1c9f22bbe580b976d1f7 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-08-23 22:07:46 |
Source code size: | 2044 bytes / 48 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 571 / 542 |
Referenced in: | #1006654 - Standard functions list 2 (LIVE, continuation of #761) |