2016-04-08 3 views
0

ich eine Endpoints API-Methode, die mir eine Upload-URL zu Google Cloud Storage wie dies gibt:Upload zu Google Cloud Storage mit Blobstore API mit HttpURLConnection in Android

@ApiMethod(name = "getUploadUrl", path = "get_upload_url", httpMethod = ApiMethod.HttpMethod.POST) 
    public CollectionResponse<String> getUploadUrl(@Named("objectName")String objectName){ 

     BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
     String callbackUrl = "/handleupload"; 
     String uploadUrl = blobstoreService.createUploadUrl(callbackUrl, 
       UploadOptions.Builder.withGoogleStorageBucketName("my-bucket")); 

     ArrayList<String> results = new ArrayList<>(1); 
     results.add(uploadUrl); 
     return CollectionResponse.<String>builder().setItems(results).build(); 
    } 

Diese erfolgreich eine URL gibt für mich zum Hochladen meine Bilddatei zu.

In Android, ich versuche, die Datei wie folgt zu laden:

private Boolean uploadImage(File file, String uploadUrl){ 
     try { 
      long bytes = file.length(); 

      URL url = new URL(uploadUrl); 
      HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setDoInput(true); 
      urlConnection.setDoOutput(true); 
      urlConnection.setRequestProperty("Connection", "Keep-Alive"); 
      urlConnection.setRequestProperty("Content-Type", "image/jpg"); 

      DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream()); 

      int bytesAvailable = 0; 
      FileInputStream fileInputStream = null; 
      try { 
       fileInputStream = new FileInputStream(file); 
       bytesAvailable = fileInputStream.available(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
       return false; 
      } 

      int maxBufferSize = 1024; 
      int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      byte[ ] buffer = new byte[bufferSize]; 

      int bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      while (bytesRead > 0) 
      { 
       outputStream.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable,maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0,bufferSize); 
      } 
      int responseCode = urlConnection.getResponseCode(); 

      fileInputStream.close(); 
      outputStream.flush(); 
      outputStream.close(); 

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

     return false; 
    } 

Aber ich wieder eine Antwort von 400 Bad Request. Ich denke, ich mache etwas falsch.

Ich möchte die HttpURLConnection Klasse verwenden, weil Android es über HttpClient empfiehlt (das ist, was die meisten SO Beiträge Beispiele haben): http://android-developers.blogspot.com/2011/09/androids-http-clients.html

Und ich bin mit dem Blobstore api ein Bild anstatt eines Google hochzuladen Cloud Storage Signed URL, weil ich eine Callback-Server-Seite für den Upload abrufen kann.

Antwort

0

Ich habe es nie selbst versucht, also bin ich mir nicht 100% sicher. Die öffentliche documentation schlägt jedoch vor, dass die Anforderung "ein Datei-Upload-Feld enthalten muss, und der Enktyp des Formulars muss auf multipart/form-data gesetzt sein." Es gibt verschiedene Beispiele, die zeigen, wie eine solche Anfrage mit Hilfe von HttppUrlConnection gesendet wird . (Dies ist eine example, aber stellen Sie sicher, dass Sie "Datei" anstelle von "Dateiname" verwenden).

Verwandte Themen