如何将InputStream上传到HttpURLConnection。

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

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

  1. String uURL = "http:some_domain/add";
  2. URL url = new URL(uURL);
  3. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  4. connection.setDoOutput(true);
  5. connection.setRequestProperty("Content-Type", "text/plain");
  6. connection.setRequestMethod("PUT");
  7. OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
  8. 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

  1. String uURL = "http:some_domain/add";
  2. URL url = new URL(uURL);
  3. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  4. connection.setDoOutput(true);
  5. connection.setRequestProperty("Content-Type", "text/plain");
  6. connection.setRequestMethod("PUT");
  7. OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
  8. 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)

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

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

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

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

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

If your content is binary file you can use DataOutputStream

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

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:

确定