JAVA POST 请求带有文件和参数的 MultiFormData

标签 java php rest post multipartform-data

我正在尝试使用“multipart/form-data”发出 POST 请求,我需要发布一个文件(下面的代码)和 4 个参数(名称、类别...)所有字符串。

我已经可以使用下面的代码发送文件,但不能使用参数。

    // open a URL connection to the Servlet
    FileInputStream fileInputStream = new FileInputStream(sourceFile);
    URL url = new URL(upLoadServerUri);

    // Open a HTTP  connection to  the URL
    conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true); // Allow Inputs
    conn.setDoOutput(true); // Allow Outputs
    conn.setUseCaches(false); // Don't use a Cached Copy
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
    conn.setRequestProperty("fileToUpload", fileName);

    dos = new DataOutputStream(conn.getOutputStream());

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"fileToUpload\";filename=" + fileName + "" + lineEnd);
    dos.writeBytes(lineEnd);

    // create a buffer of  maximum size
    bytesAvailable = fileInputStream.available();

    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // read file and write it into form...
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0) {

        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    }

    // send multipart form data necesssary after file data...
    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    serverResponseCode = conn.getResponseCode();
    String serverResponseMessage = conn.getResponseMessage();

    Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);

    if (serverResponseCode == 200) {

        runOnUiThread(new Runnable() {
            public void run() {

                Toast.makeText(PerdidosEAchados.this, "File Upload Complete.",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    //close the streams //
    fileInputStream.close();
    dos.flush();
    dos.close();

服务器代码

<?php
echo $_POST["Name"]) ;
echo $_POST["category "]) ;
?>

我尝试添加

dos.writeBytes("Content-Disposition: form-data; name=\"Name\";" + lineEnd);
                dos.writeBytes(lineEnd);
dos.writeBytes(Variable);

但是服务器从来不注册参数,我该如何解决这个问题?

最佳答案

也许您应该像 forrowing 演示代码一样构建 POST 请求?希望有帮助。

### Send a form with the text and file fields
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="Name"

myName

--WebAppBoundary
Content-Disposition: form-data; name="category"

myCategory

--WebAppBoundary
Content-Disposition: form-data; name="data"; filename=".gitignore"
Content-Type: application/json

< ./.gitignore
--WebAppBoundary--

<> 2019-09-23T045805.200.json

###

关于JAVA POST 请求带有文件和参数的 MultiFormData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58058481/

相关文章:

java - 防止 Jetty 中 session 的最后访问时间更新

java - 将原始音频编码为 mp3/ogg

php - 如何在 Vue.js 中从 vue 表单发布到 php 文件

Java:如何在api测试中传递rest api调用的 session id?

asp.net-mvc - 我可以对单个 RESTful Url 的 POST 和 GET 使用两种不同的模型吗?

java - 使用 REST 的 neo4j java API 快速示例

java - Android 上的 Google map API 无法正常工作

java - Java 8 中的空安全集合作为流

php - 来自 php 的 Highcharts 多个系列 json

javascript - 添加新内容时滚动到页面底部(不涉及 AJAX)