Skip to content

Instantly share code, notes, and snippets.

@nalf3in
Last active July 2, 2020 20:57
Show Gist options
  • Save nalf3in/a32a1af966f021c8e1e719f7c09f879d to your computer and use it in GitHub Desktop.
Save nalf3in/a32a1af966f021c8e1e719f7c09f879d to your computer and use it in GitHub Desktop.
Little class to upload a binary file to an http server
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
// For https://www.reddit.com/r/javahelp/comments/hjzr16/javagroovy_script_for_uploading_a_file_in_chunks/
public class FileUploader {
// From: https://stackoverflow.com/questions/2469451/upload-files-from-java-client-to-a-http-server
public static void main(String[] args) throws Exception {
String url = "https://httpbin.org/post";
String charset = "UTF-8";
File binaryFile = new File("lil_file.pro");
String boundary = Long.toHexString(System.currentTimeMillis()); // 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);
// Using the try with ressource construct
try (OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);) {
// Send binary file.
writer.append("--" + boundary).append(CRLF);
writer.append(
"Content-Disposition: form-data; name=\"binaryFile\"; 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();
Files.copy(binaryFile.toPath(), 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();
}
// Request is lazily fired whenever you need to obtain information about
// response.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
System.out.println(responseCode); // Should be 200
printFromStream(((HttpURLConnection) connection).getInputStream());
}
// From: Java Network Programming, 4th Edition. by Elliotte Rusty Harold p.234
private static void printFromStream(InputStream raw) throws IOException {
try (InputStream buffer = new BufferedInputStream(raw)) {
Reader reader = new InputStreamReader(buffer);
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
}
}
}
mortal(X):-person(X).
person(plato).
person(socrate).
person(zeno).
person(aristototle).
mortal_report:-
write("Known mortals are"),nl,
mortal(X),
write(X),nl,
fail.
200
{
"args": {},
"data": "",
"files": {
"binaryFile": "mortal(X):-person(X).\r\nperson(plato).\r\nperson(socrate).\r\nperson(zeno).\r\nperson(aristototle).\r\n\r\nmortal_report:-\r\n write(\"Known mortals are\"),nl,\r\n mortal(X),\r\n write(X),nl,\r\n fail.\r\n\r\n"
},
"form": {},
"headers": {
"Accept": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"Content-Length": "363",
"Content-Type": "multipart/form-data; boundary=173113555f4",
"Host": "httpbin.org",
"User-Agent": "Java/1.8.0_252",
"X-Amzn-Trace-Id": "Root=1-5efe42e7-53ca46de7ee58a318cdd0e2a"
},
"json": null,
"origin": "105.300.51.17",
"url": "https://httpbin.org/post"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment