如何在android中发送带文件的参数

Om3*_*3ga 7 php java android

我在android上传图片.目前我的代码只上传文件,但我也想发送一些参数.我正在努力追随

FileInputStream fileInputStream = new FileInputStream(sourceFile);
        URL url = new URL(upLoadServerUri);
        conn = (HttpURLConnection) url.openConnection(); // Open a HTTP  connection to  the URL
        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("uploaded_file", fileName);
        dos = new DataOutputStream(conn.getOutputStream());

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

        //Sending data
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(globalUID);
Run Code Online (Sandbox Code Playgroud)

在服务器端我使用的是PHP.这是我试图获得该参数的方式

$param = $_POST["paramName"];
target_path1 = "./places_photos/" . $param;
Run Code Online (Sandbox Code Playgroud)

但我当前的代码确实上传了文件,但它没有发送参数.如何发送参数以及如何在服务器端获取参数?

更新

目前,图像保存places_photos$target_path1变量中提到的目录中.我想要的是将该图像保存在用户的目录中,该目录被命名为用户ID.但不幸的是,我没有在服务器端获得用户ID.如何将userid与文件一起发送到服务器?

azg*_*fer 0

在此之后应该添加另一个 lineEnd:

dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd);
    dos.writeBytes(lineEnd);
    dos.writeBytes(globalUID);
    dos.writeBytes(lineEnd); <-- add this
Run Code Online (Sandbox Code Playgroud)

另请确保添加多部分边界的末尾(以“--”开头的行)在您的情况下,添加以下内容:

dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Run Code Online (Sandbox Code Playgroud)