Search code examples
javamultipartform-datahttpurlconnectionsophoslabs-intelix

POST file (as binary stream) using java.net.HttpURLConnection as a param file=<file binary stream>


I am trying to upload (POST) a file to an endpoint using java.net.HttpURLConnection but I keep getting http code 400 (bad request).

I refered to Send File And Parameters To Server With HttpURLConnection in android API 23

but problem is that I need to send this file as request body param (file=).

Note: The files will be of small size only (4-5mb) so I am reading it entirely in memory.

Corresponding curl request is:

curl -X POST "API" -H "Content-Type: multipart/form-data" -F "file="


Excerpts of Code that I am using:

    Proxy webproxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(" 
                                <proxy host>", <proxy_port>));
    HttpURLConnection http_conn = (HttpURLConnection) 
                                     url.openConnection(webproxy);
    String authorization = getAuthorization(access_token);
    http_conn.setRequestMethod("POST");
    http_conn.setRequestProperty("Accept-Charset", "UTF-8");        
    http_conn.setRequestProperty("Authorization", authorization);
    http_conn.setRequestProperty("Connection", "Keep-Alive");
    http_conn.setRequestProperty("Content-Type", "multipart/form-data);  
    http_conn.setDoOutput(true);
    http_conn.setDoInput(true);
    DataOutputStream outputStream;
    outputStream = new DataOutputStream(http_conn.getOutputStream());
    File file_obj = new File(this.file);                                
    byte[] allBytes = new byte[(int) file_obj.length()];
    FileInputStream fileInputStream = new FileInputStream(file_obj);                
    outputStream.write("file=".getBytes("UTF-8")); <---Trying to add file param here
    fileInputStream.read(allBytes);
    outputStream.write(allBytes);

Post that I just read response using below piece of code (works fine for different GET requests):

    InputStream inputStream = http_conn.getInputStream();            
        BufferedReader bufferedReader = new BufferedReader(new 
    InputStreamReader(inputStream));
    String line = "";            
    while ((line = bufferedReader.readLine()) != null) {
        data = data + line;                
    }

Note: I use java rarely an am not very familiar with it so please be descriptive in your response.


Solution

  • The above didnt worked for me so I switched to different package (okhttp3), here is what worked for me:

        File file_obj = new File(this.file); 
        String authorization = "my authorization string";
        Proxy webproxy = new Proxy(Proxy.Type.HTTP, new 
              InetSocketAddress("proxy", <port>));
    
        OkHttpClient client = new OkHttpClient.Builder().proxy(webproxy).build();      
    
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "filename",
            RequestBody.create(MediaType.parse("application/octet-stream"), file_obj)).build();
    
        Request request = new Request.Builder().header("Authorization", authorization).url(this.url).post(requestBody).build();
    
        try (Response response = client.newCall(request).execute()){
            if(!response.isSuccessful()) return "NA";
            return (response.body().string());
        }