2016-06-09 131 views
0

這個想法是從我的android應用程序使用「POST」將文件上傳到我的PHP到Google雲端存儲。 Android - > php - > GCS。 當我測試我的PHP上傳圖像到GCS,它工作正常。但是,當我嘗試上傳使用我的android應用程序服務器日誌顯示沒有錯誤,但我上傳的文件或圖像不顯示。這裏是我的代碼將圖像上傳到GCS使用PHP

安卓

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

?> 

我真的不知道我在哪裏出了錯,我真的需要幫助。

回答

0

發現了問題...這是一個在「S」,我在我的Android側忘記在「uploaded_files」結束

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