Created
November 23, 2017 18:07
-
-
Save gabrieldewes/1f3ca1d45186288ec4b9917a3ef1dfbd to your computer and use it in GitHub Desktop.
Uploading some binary file to http server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.nio.file.Files; | |
public class UploadResource { | |
// Uploading some binary file to http server | |
public static void main(String[] args) { | |
uploadFile(new File("some/path/to/file.txt")); | |
} | |
static void uploadFile(File file) { | |
if ( !file.exists()) return; | |
String serverUrl = "http://127.0.0.1"; | |
String charset = "UTF-8"; | |
String boundary = Long.toHexString(System.currentTimeMillis()); | |
String CRLF = "\r\n"; | |
try { | |
// Establishing connection with server | |
URLConnection connection = new URL(serverUrl).openConnection(); | |
connection.setDoOutput(true); | |
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); | |
try ( | |
// Opening output stream with server | |
OutputStream outputStream = connection.getOutputStream(); | |
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream, charset), true) ) | |
{ | |
// Writing header data to server | |
printWriter.append("--") | |
.append(boundary) | |
.append(CRLF); | |
printWriter.append("Content-Disposition: form-data; name=\"fileIndexName\"; filename=\"") | |
.append(file.getName()) | |
.append("\"") | |
.append(CRLF); | |
printWriter.append("Content-Type: ") | |
.append(URLConnection.guessContentTypeFromName(file.getName())) | |
.append(CRLF); | |
printWriter.append("Content-Transfer-Encoding: binary") | |
.append(CRLF); | |
printWriter.append(CRLF).flush(); | |
// Writing binary data to server output stream | |
Files.copy(file.toPath(), outputStream); | |
outputStream.flush(); | |
printWriter.append(CRLF).flush(); | |
printWriter.append("--") | |
.append(boundary) | |
.append("--") | |
.append(CRLF) | |
.flush(); | |
// Server http response code | |
int responseCode = ((HttpURLConnection) connection).getResponseCode(); | |
// Buffering response body | |
BufferedReader bufferedReader = new BufferedReader( | |
new InputStreamReader((connection.getInputStream()))); | |
StringBuilder responseBody = new StringBuilder(); | |
String responseBodyLine; | |
while ((responseBodyLine = bufferedReader.readLine()) != null) { | |
responseBody.append(responseBodyLine); | |
} | |
System.out.println("Server returned http status " | |
+ responseCode | |
+ " from url " | |
+ serverUrl | |
+ " with response body " | |
+ responseBody.toString()); | |
} | |
} catch (Exception ex) { | |
// Http Status >= 500 got here | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment