如何将InputStream上传到HttpURLConnection。

huangapple go评论59阅读模式
英文:

How to upload InputStream to HttpURLConnection

问题

I have created a HttpURLConnection using which i need to upload a file to a specific URL. Below is my code

String uURL = "http:some_domain/add";
URL url = new URL(uURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestMethod("PUT");

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

InputStream iStream = getContent("ABC", "test.json");   

Now, i have retrieved my content in iStream and want to upload it using OutputStreamWriter. How can i upload the content?

英文:

I have created a HttpURLConnection using which i need to upload a file to a specific URL. Below is my code

String uURL = "http:some_domain/add";
URL url = new URL(uURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestMethod("PUT");

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

InputStream iStream = getContent("ABC", "test.json");   

Now, i have retrieved my content in iStream and want to upload it using OutputStreamWriter. How can i upload the content?

答案1

得分: 2

如果您的内容是文本文件,您可以使用OutputStreamWriter。(看起来您的文件是一个json)

try (OutputStream os = con.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8")) {
    String data = "{\"name\": \"John\", \"age\": 30}";
    osw.write(data);
    osw.flush();
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
} catch (IOException e) {
    throw a RuntimeException(e);
}

如果您的内容是二进制文件,您可以使用DataOutputStream

try (OutputStream os = con.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os)) {
    byte[] data = {0x01, 0x02, 0x03, 0x04, 0x05};
    dos.write(data);
    dos.flush();
} catch (IOException e) {
    throw new RuntimeException(e);
}
英文:

If your content is text file you can use OutputStreamWriter. (It's look like your file is a json)

try (OutputStream os = con.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8")) {
    String data = "{\"name\": \"John\", \"age\": 30}";
    osw.write(data);
    osw.flush();
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
} catch (IOException e) {
    throw new RuntimeException(e);
}

If your content is binary file you can use DataOutputStream

try (OutputStream os = con.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os)) {
    byte[] data = {0x01, 0x02, 0x03, 0x04, 0x05};
    dos.write(data);
    dos.flush();
} catch (IOException e) {
    throw new RuntimeException(e);
}

huangapple
  • 本文由 发表于 2023年4月17日 23:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定