使用Android应用程序中的文件和文本发出POST请求

Lan*_*ler 5 php post android httpurlconnection

我在从Android应用程序向我的Web服务(Google Cloud和PHP)发出正确的POST请求时遇到了一些问题.

当我尝试从Android发送图片时,它会返回"200 OK"响应,但图片不会保存在Google云端存储上的存储桶中.我知道问题在于Android应用程序中我的方法发出的POST请求,因为我已经使用Postman进行了测试并且没有任何问题.所以我的应用程序的下面一段代码出了问题:

public void uploadFile(String uploadUrl, String filename, String newFileName) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File(filename));

        URL url = new URL(uploadUrl);
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs.
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Set HTTP method to POST.
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        outputStream = new DataOutputStream(connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"img\";filename=\"" + filename + "\"" + lineEnd);
        outputStream.writeBytes(lineEnd);

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

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0) {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

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


        System.out.println(serverResponseMessage);
        System.out.println(serverResponseCode);


        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
    }
Run Code Online (Sandbox Code Playgroud)

我已经测试过所有参数都是100%正确的,所以你不必担心.

如果您需要从Web服务中查看用于接收请求的PHP代码,请执行以下操作:

<?php
$img = $_FILES['img']['tmp_name'];
move_uploaded_file($img, 'gs://routeimages/yeeeeeeees.jpg');

echo "done";

?>
Run Code Online (Sandbox Code Playgroud)

这段代码中的问题可能出在哪里?

额外

我还需要在文件/图像之外添加一些文本.如何将此添加到请求中?

cha*_*l03 3

您似乎缺少Content-type要发送的图像文件。

查看以下用于发送文件的 HTTP Post 标头

Content-type: multipart/form-data, boundary=AaB03x
    --AaB03x
    content-disposition: form-data; name="pics", filename="file2.gif"
    Content-type: image/gif
    Content-Transfer-Encoding: binary

    ...contents of file2.gif...
    --AaB03x--
Run Code Online (Sandbox Code Playgroud)

因此,您需要Content-type在代码中添加属性,如下所示:

outputStream = new DataOutputStream(connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
outputStream.writeBytes("Content-Disposition: form-data; name=\"img\";filename=\"" + filename + "\"" + lineEnd);
outputStream.writeBytes("Content-type: image/gif" + lineEnd); // or whatever format you are sending.
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd); 
outputStream.writeBytes(lineEnd);
Run Code Online (Sandbox Code Playgroud)

我希望这段代码能够帮助您解决问题。

附加:除了文件/图像之外,我还需要添加一些文本。我如何将其添加到请求中? 要发送一些额外的参数(一些文本),请查看以下发送图像和文本参数的 http 格式:

    Content-type: multipart/form-data, boundary=AaB03x
    --AaB03x
    content-disposition: form-data; name="pics", filename="file2.gif"
    Content-type: image/gif
    Content-Transfer-Encoding: binary

    ...contents of file2.gif...
    --AaB03x--
    content-disposition: form-data; name="field1"

    Joe Blow
    --AaB03x
Run Code Online (Sandbox Code Playgroud)

我希望您现在可以借助上面的图像上传说明和此格式编写额外参数的代码。