使用HTTP Post上传文件-Java

时间:2018-10-26 09:34:47

标签: java

我有这个当前代码。该文件位于InputStream intest.pdf上的内存中。我希望只保留在内存中。

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();

如何将此文件发布到/submit URL。我在这里想念什么?

0 个答案:

没有答案