使用PHP将图像上传到GCS

时间:2016-06-09 18:09:19

标签: php android google-cloud-storage

我的想法是使用" POST"从我的Android应用程序上传文件。到我的PHP到谷歌云存储。 Android - > php - > GCS。 当我测试我的PHP上传图像到GCS,它工作正常。但是,当我尝试使用我的Android应用程序上传时,服务器日志显示没有错误,但我上传的文件或图像不会显示。这是我的代码

机器人:

public int uploadFile(final String selectedFilePath) {

    int serverResponseCode = 0;

    HttpURLConnection connection;
    DataOutputStream dataOutputStream;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";


    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File selectedFile = new File(selectedFilePath);


    String[] parts = selectedFilePath.split("/");
    final String fileName = parts[parts.length - 1];


    try {
        FileInputStream fileInputStream = new FileInputStream(selectedFile);
        URL url = new URL(KEY_ONLINE_UPLOAD_IMAGE);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);//Allow Inputs
        connection.setDoOutput(true);//Allow Outputs
        connection.setUseCaches(false);//Don't use a cached Copy
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("ENCTYPE", "multipart/form-data");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        connection.setRequestProperty("uploaded_file", selectedFilePath);

        //creating new dataoutputstream
        dataOutputStream = new DataOutputStream(connection.getOutputStream());

        //writing bytes to data outputstream
        dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
        dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                + selectedFilePath + "\"" + lineEnd);

        dataOutputStream.writeBytes(lineEnd);

        //returns no. of bytes present in fileInputStream
        bytesAvailable = fileInputStream.available();
        //selecting the buffer size as minimum of available bytes or 1 MB
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        //setting the buffer as byte array of size of bufferSize
        buffer = new byte[bufferSize];

        //reads bytes from FileInputStream(from 0th index of buffer to buffersize)
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        //loop repeats till bytesRead = -1, i.e., no bytes are left to read
        while (bytesRead > 0) {
            //write the bytes read from inputstream
            dataOutputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

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

        serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();

        Log.i(KEY_CLASS_NAME, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode);


        //closing the input and output streams
        fileInputStream.close();
        dataOutputStream.flush();
        dataOutputStream.close();


    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return serverResponseCode;
}

add_image.php:

require("connection.inc.php");
use google\appengine\api\cloud_storage\CloudStorageTools;

//if posted data is not empty
if (!empty($_POST)) {
    $my_bucket = 'mypersonalbucket';
    $options = ['gs_bucket_name' => $my_bucket];
    $upload_url =CloudStorageTools::createUploadUrl('/android/users/image/con_image.php', $options);


} else {
?>
<form action="<?php
$my_bucket = 'mypersonalbucket';
$options = ['gs_bucket_name' => $my_bucket];
$upload_url = CloudStorageTools::createUploadUrl('/android/users/image/con_image.php', $options);
echo $upload_url; ?>"
      enctype="multipart/form-data" method="post">

    Files to upload: <br>
    <input type="file" name="uploaded_files" size="40">
    <input type="submit" value="Send">

</form>
<?php
}

?>

con_image.php:

var_dump($_FILES);
$my_bucket = 'mypersonalbucket';
echo $file_name = $_FILES['uploaded_files']['name'];
echo $temp_name = $_FILES['uploaded_files']['tmp_name'];
echo move_uploaded_file($temp_name, "gs://${my_bucket}/users/profile_images/${file_name}");

?>

我真的不知道哪里出错了,我真的需要帮助。

1 个答案:

答案 0 :(得分:0)

发现问题......我的机器人面板上的“uploaded_files”末尾我忘记了“s”

    dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_files\";filename=\""
            + selectedFilePath + "\"" + lineEnd);