在 post 方法中以 https url 上传文件 - java.io.IOException:写入服务器时出错 - Java [英] Uploading file in a https url in post method - java.io.IOException: Error writing to server - Java

查看:38
本文介绍了在 post 方法中以 https url 上传文件 - java.io.IOException:写入服务器时出错 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to upload a binary image file to the server using multi-part, I open the connection to the https url and write diretly to to the outputstream. I have tried the SSLSocket, apache http client, but in all ways I get the error message, java.io.IOException: Error writing to server. I have to write post params with the multi-part binary file. The code is given below, where am I failing to see the mistake in this code below? And, I have given the sample data to write the data to the connection. I would be glad if anyone could give some idea how to do this with apache http client, which I do not know.

示例数据包

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)

Exception

java.io.IOException: Error writing to server at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:468) at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:480) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1070) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)

推荐答案

您可以在 Apache HttpClient 示例页面.

You can find the basic example for a "chunk encoded POST" on the Apache HttpClient example page.

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

For the form-based post you also need org.apache.httpcomponents:httpmime:4.3.2
With that you get the org.apache.http.entity.mime.MultipartEntityBuilder which you can use like so:

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

这应该对您有所帮助.

这篇关于在 post 方法中以 https url 上传文件 - java.io.IOException:写入服务器时出错 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆