簡體   English   中英

在post方法中的https url中上傳文件-java.io.IOException:寫入服務器時出錯-Java

[英]Uploading file in a https url in post method - java.io.IOException: Error writing to server - Java

我必須使用多部分程序將二進制圖像文件上傳到服務器,打開與https url的連接,然后直接寫入輸出流。 我已經嘗試過SSLSocket和apache http客戶端,但是在所有方面我都收到錯誤消息java.io.IOException:寫入服務器時出錯。 我必須使用多部分二進制文件編寫post params。 下面給出了代碼,下面的代碼在哪里我看不到錯誤? 並且,我給出了示例數據以將數據寫入連接。 如果有人可以給出一些如何使用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)處寫入sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:468)上的服務器時出錯: 480),位於sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1070),位於sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)

您可以在Apache HttpClient 示例頁面上找到“塊編碼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());

那應該對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM