java-curl post with wave-binary from wave file

时间:2016-12-15 09:34:07

标签: java http curl post wave

我想在正文上使用数据二进制文件发出POST请求。在GNU / Linux shell中,此命令完美运行:

curl -v -X POST "http://13.75.XXX.XXX:1000"  --data-binary  @test.wav

我尝试使用以下代码在java中发出此请求:

    String url = "http://13.75.XXX.XXX:1000";
    String charset = "UTF-8";
    String param = "value";
    File textFile = new File("/Users/maxime/Desktop/test.wav");
    File binaryFile = new File("/Users/maxime/Desktop/test.wav");
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
    String CRLF = "\r\n"; // Line separator required by multipart/form-data.

    URLConnection connection = new URL(url).openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

    try (
            OutputStream output = connection.getOutputStream();
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
    ) {
        // Send normal param.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
        writer.append(CRLF).append(param).append(CRLF).flush();

        // Send text file.
        //writer.append("--" + boundary).append(CRLF);
        //writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); // Text file itself must be saved in this charset!
        //writer.append(CRLF).flush();
        //Files.copy(textFile.toPath(), output);
        //output.flush(); // Important before continuing with writer!
        //writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

        // Send binary file.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
        writer.append("Content-Transfer-Encoding: binary").append(CRLF);
        writer.append(CRLF).flush();
        Files.copy(binaryFile.toPath(), output);
        output.flush(); // Important before continuing with writer!
        writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.

        // End of multipart/form-data.
        writer.append("--" + boundary + "--").append(CRLF).flush();
    }

    // Request is lazily fired whenever you need to obtain information about response.
    int responseCode = ((HttpURLConnection) connection).getResponseCode();
    System.out.println(responseCode); // Should be 200
}

我的服务器获取此请求,但如果来自Curl或我的java代码,则二进制数据不相同。 如何在我的请求中发送正确的数据?

0 个答案:

没有答案