diff --git a/java_console/.idea/libraries/httpclient.xml b/java_console/.idea/libraries/httpclient.xml new file mode 100644 index 0000000000..267008a6e0 --- /dev/null +++ b/java_console/.idea/libraries/httpclient.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/java_console/lib/httpclient-javadoc.jar b/java_console/lib/httpclient-javadoc.jar new file mode 100644 index 0000000000..550fbcb26e Binary files /dev/null and b/java_console/lib/httpclient-javadoc.jar differ diff --git a/java_console/lib/httpclient.jar b/java_console/lib/httpclient.jar new file mode 100644 index 0000000000..300cdf0f90 Binary files /dev/null and b/java_console/lib/httpclient.jar differ diff --git a/java_console/lib/httpcore.jar b/java_console/lib/httpcore.jar new file mode 100644 index 0000000000..163dc438cb Binary files /dev/null and b/java_console/lib/httpcore.jar differ diff --git a/java_console/lib/httpmime-javadoc.jar b/java_console/lib/httpmime-javadoc.jar new file mode 100644 index 0000000000..e61e6fc2a3 Binary files /dev/null and b/java_console/lib/httpmime-javadoc.jar differ diff --git a/java_console/lib/httpmime.jar b/java_console/lib/httpmime.jar new file mode 100644 index 0000000000..f7e5dbf126 Binary files /dev/null and b/java_console/lib/httpmime.jar differ diff --git a/java_console/ui/src/com/rusefi/tools/online/Online.java b/java_console/ui/src/com/rusefi/tools/online/Online.java index 747ffaa2ff..0c40db33be 100644 --- a/java_console/ui/src/com/rusefi/tools/online/Online.java +++ b/java_console/ui/src/com/rusefi/tools/online/Online.java @@ -1,64 +1,37 @@ package com.rusefi.tools.online; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.mime.MultipartEntity; +import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.entity.mime.content.StringBody; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.util.EntityUtils; + import java.io.*; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLConnection; -import java.nio.file.Files; public class Online { - static final String charset = "UTF-8"; - static final String url = "https://rusefi.com/online/upload.php"; - static String CRLF = "\r\n"; // Line separator required by multipart/form-data. + private static final String url = "https://rusefi.com/online/upload.php"; public static void upload(File xmlFile, String authTokenValue) throws IOException { - String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value. + HttpClient httpclient = new DefaultHttpClient(); + HttpPost httpPost = new HttpPost(url); - URLConnection connection = new URL(url).openConnection(); - connection.setDoOutput(true); - connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); + FileBody uploadFilePart = new FileBody(xmlFile); + MultipartEntity reqEntity = new MultipartEntity(); + reqEntity.addPart("upload-file", uploadFilePart); + reqEntity.addPart("auth_token", new StringBody(authTokenValue)); - try ( - OutputStream output = connection.getOutputStream(); - PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true); - ) { - sendParameter(boundary, writer, "auth_token", authTokenValue); + httpPost.setEntity(reqEntity); - // Send text file. - writer.append("--" + boundary).append(CRLF); - writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + xmlFile.getName() + "\"").append(CRLF); - writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset! - writer.append(CRLF).flush(); - Files.copy(xmlFile.toPath(), output); - output.flush(); // Important before continuing with writer! - writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary. + HttpResponse response = httpclient.execute(httpPost); + System.out.println(response); - // End of multipart/form-data. - writer.append("--" + boundary + "--").append(CRLF).flush(); - } + HttpEntity entity = response.getEntity(); + String responseString = EntityUtils.toString(entity, "UTF-8"); + System.out.println(responseString); - - HttpURLConnection httpURLConnection = (HttpURLConnection) connection; - int responseCode = httpURLConnection.getResponseCode(); - System.out.println(responseCode); // Should be 200 - - BufferedReader br; - if (responseCode >= 200 && responseCode <= 299) { - br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); - } else { - br = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream())); - } - String output; - while ((output = br.readLine()) != null) { - System.out.println(output); - } - } - - private static void sendParameter(String boundary, PrintWriter writer, String parameterName, String value) { - // Send normal param. - writer.append("--" + boundary).append(CRLF); - writer.append("Content-Disposition: form-data; name=\"" + parameterName +"\"").append(CRLF); - writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); - writer.append(CRLF).append(value).append(CRLF).flush(); } } diff --git a/java_console/ui/ui.iml b/java_console/ui/ui.iml index 523a616609..b8a3994949 100644 --- a/java_console/ui/ui.iml +++ b/java_console/ui/ui.iml @@ -18,5 +18,6 @@ + \ No newline at end of file