2

I have this current code. The file is in memory on the InputStream in or in test.pdf. I would prefer to only keep in-memory.

FileOutputStream fos = new FileOutputStream(new File("test.pdf"));
// Read file
InputStream in = url.openStream();

while((bufferLength = in.read(buffer)) != -1) {
    fos.write(buffer, 0, bufferLength);
}

fos.flush();

// Close connections
fos.close();
in.close();     

System.out.println("GOT DOCUMENT");

String submitURl = "https://someURL/submit";

// Send data
HttpURLConnection conn = (HttpURLConnection) new URL(submitURl).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type","multipart/form-data");
conn.setRequestProperty("User-Agent", "Test Agent");

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();

How do I post this file to the /submit URL. What am I missing here?

cse
  • 4,066
  • 2
  • 20
  • 37
  • instead of writing to fos, writing to wr. Have you tried that and it didn't work? What is the problem here? – kutschkem Oct 26 '18 at 11:24
  • Possible duplicate of [Upload files from Java client to a HTTP server](https://stackoverflow.com/questions/2469451/upload-files-from-java-client-to-a-http-server) – rkosegi Oct 26 '18 at 11:31
  • It seems that you're trying to copy the input stream straight to the outputstream with a the writer. You can see here how this can be achieved: https://stackoverflow.com/questions/43157/easy-way-to-write-contents-of-a-java-inputstream-to-an-outputstream If we misunderstood your question, try to be more specific about your need or what you tried that didn't work. – Eric Darchis Oct 26 '18 at 11:35

0 Answers0