簡體   English   中英

使用PHP將圖像Android上傳到GCS

[英]uploading image Android to GCS using PHP

這個想法是使用“ POST”從我的Android應用程序將文件上傳到我的php到Google Cloud Storage。 Android-> PHP-> GCS。 當我測試將php上傳到GCS的php時,效果很好。 但是,當我嘗試使用Android應用上傳時,服務器日志未顯示任何錯誤,但我上傳的文件或圖像未顯示。 這是我的密碼

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}");

?>

我真的不知道哪里出問題了,我真的需要幫助。

發現了問題...這是我在Android端“ uploaded_files”末尾忘記的“ s”上

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM