1 | static void uploadFileToPhoneServer(File binaryFile) ctex { |
2 | if (binaryFile == null || !binaryFile.isFile()) ret; |
3 | |
4 | S ip = first(detectGateways()); |
5 | assertNotNull(ip); |
6 | S url = "http://" + ip + ":8888/upload"; |
7 | print("Uploading to: " + url); |
8 | new Map<S, S> params; |
9 | |
10 | String charset = "UTF-8"; |
11 | String boundary = randomID(20); // Just generate some unique random value. |
12 | String CRLF = "\r\n"; // Line separator required by multipart/form-data. |
13 | |
14 | URLConnection connection = new URL(url).openConnection(); |
15 | connection.setDoOutput(true); |
16 | connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); |
17 | |
18 | OutputStream output = connection.getOutputStream(); |
19 | PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); |
20 | |
21 | // Send normal param. (TODO: I don't think this works) |
22 | for (S param : keys(params)) { |
23 | writer.append("--" + boundary).append(CRLF); |
24 | writer.append("Content-Disposition: form-data; name=\"" + urlencode(param) + "\"").append(CRLF); |
25 | writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); |
26 | // TODO: encode?? |
27 | writer.append(CRLF).append(urlencode(params.get(param))).append(CRLF); |
28 | } |
29 | |
30 | // Send binary file. |
31 | writer.append("--" + boundary).append(CRLF); |
32 | writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF); |
33 | writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF); |
34 | writer.append("Content-Transfer-Encoding: binary").append(CRLF); |
35 | writer.append(CRLF).flush(); |
36 | file2stream(binaryFile, output); |
37 | output.flush(); // Important before continuing with writer! |
38 | writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary. |
39 | |
40 | // End of multipart/form-data. |
41 | writer.append("--" + boundary + "--").append(CRLF).flush(); |
42 | writer.close(); |
43 | output.close(); |
44 | |
45 | print("Sent."); |
46 | print(); |
47 | print(loadPage(connection, new URL(url))); |
48 | } |
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: | 570 / 542 |
Referenced in: | [show references] |