Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1003776 // Test uploading file to Phone Server

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1445L/11K/35K).

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

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003776
Snippet name: Test uploading file to Phone Server
Eternal ID of this version: #1003776/1
Text MD5: 20e06f636f5a8bf61ce53987a62a7bc6
Transpilation MD5: 7b4c1e09cdee1a18c83911249256d554
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-27 00:11:43
Source code size: 2015 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 542 / 582
Referenced in: [show references]