TK52 TK52 - 1 month ago 11
Android Question

uploading image Android to GCS using PHP

The idea is to upload a file from my android app using "POST" to my php to the Google Cloud Storage. Android -> php -> GCS.
When I test my php to upload an image to GCS, it works fine. However, when I try to upload using my android app the server log shows no error but the file or image I uploaded doesn't show up. here is my codes

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

?>


I really don't know where I went wrong, and I really need help.

Answer

Found the problem ... it was a on "s" that i forgot at the end of "uploaded_files" in my android side

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