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

58
LINES

< > BotCompany Repo | #1004790 // Upload binary data to TinyBrain - use Apache Commons URLEncoder

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

Uses 278K of libraries. Click here for Pure Java version (2321L/16K/52K).

!752

lib 1004792 // commons codec

import org.apache.commons.codec.net.URLCodec;

static boolean test;
  
p {
  S name = args[0]; // put file name here
  if (args.length > 1 && args[1].equals("test"))
    test = true;
  name = new File(name).getAbsolutePath();
  byte[] data  = loadBinaryFile(name);
  if (data == null)
    print("File not found: " + name);
  else
    if (test)
      print(binaryUrlencode(data));
    else
      uploadData("data-uploader " + programID(), "Data file " + name + " from " + computerID(), data, false);
}

static String uploadData(String gummipw, String title, byte[] data, boolean superhigh) throws IOException {
  if (gummipw == null || gummipw.length() == 0)
    throw new RuntimeException("Need gummi password.");
    
  System.out.println("Uploading:");
  System.out.println("  gummipw: " + gummipw);
  System.out.println("  " + title);
  int displayLength = 40;
  System.out.println("  " + data.length + " bytes of data");

  URL url = new URL("http://tinybrain.de:8080/tb-int/add_snippet.php");
  S base = base64encode(data);
  print("Base-encoded => " + l(base));
  data = null;
  S encoded = new URLCodec().encode(base, "UTF-8");
  base = null;
  print("URL-encoded => " + l(encoded));
  String postData =
    "gummipw=" + URLEncoder.encode(unnull(gummipw), "UTF-8")
    + "&blob=" + encoded
    + "&name=" + URLEncoder.encode(unnull(title), "UTF-8")
    + "&type=" + 37 /* SN_DATA */
    + "&" + (superhigh ? "superhigh" : "high") + "=1&public=1";
  System.out.println("Post data length: " + postData.length());

  String contents = doPost(postData, url.openConnection(), url);
  System.out.println(contents);

  if (isInteger(contents)) {
    long id = parseSnippetID(contents);
    System.out.println("=> #" + id);
    return "#" + id;
  } else
    throw new RuntimeException("Server error: " + contents);
}

Author comment

Began life as a copy of #1000755

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004790
Snippet name: Upload binary data to TinyBrain - use Apache Commons URLEncoder
Eternal ID of this version: #1004790/1
Text MD5: 8b6a5419de31b3bc5549578ee8dfcd38
Transpilation MD5: be347855725004f894f3e6232cc02fcd
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-31 05:57:39
Source code size: 1908 bytes / 58 lines
Pitched / IR pitched: No / No
Views / Downloads: 614 / 698
Referenced in: [show references]