2016-06-09 7 views
0

Die Idee ist es, eine Datei von meiner Android-App mit "POST" zu meinem php zum Google Cloud Storage zu laden. Android -> php -> GCS. Wenn ich mein php teste, um ein Bild zu GCS hochzuladen, funktioniert es gut. Wenn ich jedoch versuche, mit meiner Android-App hochzuladen, zeigt das Serverprotokoll keinen Fehler, aber die Datei oder das Bild, das ich hochgeladen habe, wird nicht angezeigt. hier ist mein CodesBild hochladen Android zu GCS mit PHP

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

?> 

Ich weiß wirklich nicht, wo ich schief gelaufen ist, und ich brauche wirklich Hilfe.

Antwort

0

Gefunden das Problem ... es war ein auf „s“, die ich am Ende der „uploaded_files“ in meiner android Seite vergessen

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