gpt4 book ai didi

java - 在 post 方法中上传 https url 中的文件 - java.io.IOException : Error writing to server - Java

转载 作者:太空宇宙 更新时间:2023-11-04 15:07:47 25 4
gpt4 key购买 nike

我必须使用多部分将二进制图像文件上传到服务器,我打开与 https url 的连接并直接写入输出流。我尝试过 SSLSocket、apache http 客户端,但在所有方面我都收到错误消息 java.io.IOException:写入服务器时出错。我必须用多部分二进制文件编写帖子参数。下面给出了代码,我在下面的代码中没有看到错误在哪里?而且,我已经给出了示例数据以将数据写入连接。如果有人能提供一些关于如何使用我不知道的 apache http 客户端执行此操作的想法,我会很高兴。

数据包数据示例

header part
--------------
--aabbaabbaabbxx
Content-Disposition: form-data; name="to"

<recipient>
--aabbaabbaabbxx
Content-Disposition: form-data; name="from"

<sender>
--aabbaabbaabbxx
Content-Disposition: form-data; name="file"; filename="<file_hash>.jpg"
Content-Type: image/jpeg

footer
-----------
--aabbaabbaabbxx--


Are these request parameters?
-----------------------------

POST <full_url>
Content-Type: multipart/form-data; boundary=aabbaabbaabbxx
Host: <host_name>
User-Agent: <agent>
Content-Length: <file_size>

代码

final URL uri = new URL(uploadUrl);
String boundary = "aabbaabbaabbxx";
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setAllowUserInteraction(false);
connection.setRequestMethod("POST");
connection.setUseCaches(false);

OutputStream writer = null;
try {
writer = connection.getOutputStream();

// post
writer.write(MessageFormat
.format("POST {0}\r\n", uploadUrl)
.getBytes("UTF-8"));
writer.write(MessageFormat
.format("Content-Type: multipart/form-data; boundary={0}\r\n",
boundary).getBytes("UTF-8"));
writer.write(MessageFormat.format("Host: {0}\r\n",uri.getHost()).getBytes("UTF-8"));
writer.write(MessageFormat.format("User-Agent: {0}\r\n",Constants.UserAgent).getBytes("UTF-8"));
writer.write(MessageFormat.format("Content-Length: {0}\r\n\r\n",clength).getBytes("UTF-8"));

// header
writer.write(("--" + boundary).getBytes("UTF-8"));
writer.write("Content-Disposition: form-data; name=\"to\"\r\n\r\n".getBytes("UTF-8"));
writer.write(MessageFormat.format("{0}\r\n", to).getBytes("UTF-8"));
writer.write(MessageFormat.format("--{0}\r\n", boundary).getBytes("UTF-8"));
writer.write("Content-Disposition: form-data; name=\"from\"\r\n\r\n".getBytes("UTF-8"));
writer.write(MessageFormat.format("{0}\r\n",this.phoneNumber).getBytes("UTF-8"));
writer.write(MessageFormat.format("--{0}\r\n", boundary).getBytes("UTF-8"));
writer.write(MessageFormat.format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n",hashname).getBytes("UTF-8"));
writer.write(MessageFormat.format("Content-Type: {0}\r\n\r\n", contenttype).getBytes("UTF-8"));

// file data
InputStream is = null;
try {
is = new FileInputStream(path);
int data = 0;
while ((data = is.read()) != -1) {
writer.write(data);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException logOrIgnore) {
}
}
}

// footer
writer.write(("--" + boundary + "--").getBytes("UTF-8"));
writer.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}

异常

java.io.IOException:写入服务器时出错 在 sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:468) 在 sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:480) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1070) 在sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)

最佳答案

您可以在 Apache HttpClient example page 上找到“ block 编码 POST”的基本示例.

对于基于表单的帖子,您还需要org.apache.httpcomponents:httpmime:4.3.2
这样你就得到了org.apache.http.entity.mime.MultipartEntityBuilder,你可以像这样使用它:

    MultipartEntityBuilder meb = MultipartEntityBuilder.create();
meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
meb.addPart("to", new StringBody("The recipient", ContentType.TEXT_PLAIN));
meb.addPart("from", new StringBody("The sender", ContentType.TEXT_PLAIN));
FileBody fb = new FileBody(new File("path/to/your/file"), ContentType.create("image/jpeg"));
meb.addPart("file", fb);
meb.addPart("footer", new StringBody("The footer", ContentType.TEXT_PLAIN));
httppost.setEntity(meb.build());

这应该对你有所帮助。

关于java - 在 post 方法中上传 https url 中的文件 - java.io.IOException : Error writing to server - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com