-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.java
76 lines (61 loc) · 2.82 KB
/
example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Ubuntu instalation
// sudo apt install default-jdk
// You can make this code simpler if you use a lib like HttpClient (https://hc.apache.org/httpcomponents-client-ga/index.html)
// examples at: https://www.baeldung.com/httpclient-post-http-request (POST Multipart Request)
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Example {
private static final String CRLF = "\r\n";
private static final String CHARSET = StandardCharsets.UTF_8.name();
public static void main(String[] args) throws Exception {
String url = "http://127.0.0.1:5000/score";
String uploadFilePath = "../data/test.csv.gz";
String outputFilePath = "output.csv.gz";
File binaryFile = new File(uploadFilePath);
String boundary = Long.toHexString(System.currentTimeMillis());
URLConnection connection = new URL(url).openConnection();
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setDoOutput(true);
try (
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, CHARSET), true);
) {
// add userid
addString(writer, boundary, "userid", "1");
// add file
addFile(writer, output, boundary, "file", binaryFile);
writer.append("--" + boundary + "--").append(CRLF).flush();
}
Files.copy(((HttpURLConnection) connection).getInputStream(), Paths.get(outputFilePath));
System.out.println("Response code: " + ((HttpURLConnection) connection).getResponseCode());
System.out.println("Response content: " + outputFilePath);
}
private static void addString(PrintWriter writer, String boundary, String paramKey, String paramValue) {
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"" + paramKey + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + CHARSET).append(CRLF);
writer.append(CRLF).append(paramValue).append(CRLF).flush();
}
private static void addFile(PrintWriter writer, OutputStream output, String boundary, String paramKey,
File binaryFile) throws IOException {
writer.append("--" + boundary).append(CRLF);
writer.append(
"Content-Disposition: form-data; name=\"" + paramKey + "\"; 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();
writer.append(CRLF).flush();
}
}