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).

!752

p {
  S ip = first(detectGateways());
  assertNotNull(ip);
  bool test = true;
  S url = "http://" + ip + ":8888/upload";
  print("Uploading to: " + url);
  new Map<S, S> 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)));
}

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: 594 / 651
Referenced in: #1003778 - uploadFileToPhoneServer