0

I have a problem with my Post of a file to a REST API. The documentInputStream comes from a previous function to get the InputStream of a file and it works (it writes the file to my filesystem.

I also checked while debugging and documentInputStream has count = 35125 which is my file size (35Kb)

But the server is not getting any file. (This is not a server issue because it works with postman).

Any help?

  // Send document
    HttpURLConnection submitConnection = (HttpURLConnection) new URL(submitURL).openConnection();
    submitConnection.setRequestMethod("POST");
    submitConnection.setDoOutput(true);
    submitConnection.setUseCaches(false);
    // creates a unique boundary based on time stamp
    String boundary = "===" + System.currentTimeMillis() + "===";
    submitConnection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
    submitConnection.setRequestProperty("User-Agent", "Test Agent");

    // Get output stream
    DataOutputStream dataStream = new DataOutputStream(submitConnection .getOutputStream()); 

    // Write file input stream to the connection stream
    IOUtils.copy(documentInputStream, dataStream);
    // Flush
    dataStream.flush();

    // Get the response 
    BufferedReader rd = new BufferedReader(new InputStreamReader(submitConnection.getInputStream()));

0 Answers0