如何写入 HTTP 正文消息参数和 zip 文件?

How to write to HTTP body message parameters AND a zip file?

我不知道如何正确 post 请求正文中的参数以及 zip 文件。

这是我目前所做的:

    HttpsURLConnection connection = openConnection(requestURL);
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

    Map<String, String> keyValueMap = new HashMap<String, String>();
    keyValueMap.put("method", "1");
    keyValueMap.put("apiKey", "2");
    keyValueMap.put("merchantUUID", "3");
    keyValueMap.put("batchNotifyURL", "4");
    keyValueMap.put("fileHash", "5");
    keyValueMap.put("urlHash", "6");

    PrintWriter writer = null;
    try {
        writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));

        for(String key : keyValueMap.keySet()) {
            writer.println("--" + boundary);
            writer.println("Content-Disposition: form-data; name=\"" + key + "\"");
            writer.println("Content-Type: text/plain; charset=UTF-8");
            writer.println();
            writer.println(keyValueMap.get(key));
        }
        writer.println("--" + boundary);
        writer.println("Content-Disposition: form-data; name=\"batchFile\"; filename=\"" + fileName + "\"");
        writer.println("Content-Type: text/plain; charset=UTF-8");
        writer.println();

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileToUpload), "UTF-8"));
            for (String line; (line = reader.readLine()) != null;) {
                writer.println(line);
            }
        } finally {
            if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
        }
        writer.println("--" + boundary + "--");
    } finally {
        if (writer != null) writer.close();
    }

我收到的错误信息是: 提交的文件不是有效的 zip 存档。

还有其他方法可以做到这一点吗?我错过了什么吗?

谢谢。

问题已解决。 以下是我的解决方案:

    OutputStream outputStream = connection.getOutputStream();
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET), true);
    try {
        for(String key : keyValueMap.keySet()) {
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"" + key + "\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + CHARSET).append(CRLF);
            writer.append(CRLF).append(keyValueMap.get(key)).append(CRLF).flush();
        }
        // Send binary file.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"batchFile\"; filename=\"" + fileToUpload.getName() + "\"").append(CRLF);
        writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileToUpload.getName())).append(CRLF);
        writer.append("Content-Transfer-Encoding: binary").append(CRLF);
        writer.append(CRLF).flush();
        Files.copy(fileToUpload.toPath(), outputStream);
        outputStream.flush(); // Important before continuing with writer!
        writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
        writer.println("--" + boundary + "--");
    } finally {
        if (writer != null) writer.close();
    }