2016-05-24 21 views
1

Ich versuche eine Datei mit Box API in Box hochzuladen.BOX API Hochladen einer Datei Java

Aber was immer ich versuche, ich bekomme immer 400 Bad Request ohne weitere Informationen.

Irgendeine Idee über das Problem?

Das Beispiel von der API ist dies curl Anfrage:

curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer access_token" -X POST \
-F attributes = '{ "name":“ tigers.jpeg“, "Eltern": { "id": "11446498"}}‘ \
-F [email protected]

Mein Code ist unten:

String URL = "https://upload.box.com/api/2.0/files/content/"; 

    HttpClient httpClient = new HttpClient(); 
    PostMethod postMethod = new PostMethod(URL); 
    postMethod.setRequestHeader("Authorization", "Bearer "+ this.token); 

    try { 
     List<Part> parts = new ArrayList<Part>(); 

     JSONObject parent = new JSONObject(); 
     parent.put("id", this.parentId); 

     JSONObject attributes = new JSONObject(); 
     attributes.put("parent", parent); 
     attributes.put("name", file.getName()); 

     StringPart strPart = new StringPart("attributes", attributes.toString()); 
     strPart.setContentType("application/json"); 
     parts.add(strPart); 

     ByteArrayPartSource source = new ByteArrayPartSource(file.getName(), 
       IOUtils.toByteArray(this.file); 
     parts.add(new FilePart("file", source)); 

     postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams())); 
     httpClient.executeMethod(postMethod); 

     int status = postMethod.getStatusCode(); 

     if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_ACCEPTED) { 
      String jsonText = postMethod.getResponseBodyAsString(); 
      JSONObject json = new JSONObject(jsonText); 
      System.out.println(jsonText); 
     } else { 
      throw new MyException(postMethod.getResponseBodyAsString()); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     postMethod.releaseConnection(); 
    } 

Antwort

0

Haben Sie die übergeordnete ID validiert = 11446498 ist gültig? Wenn Sie diesen Endpunkt nur testen, versuchen Sie es mit der ID = 0, die den Stammordner darstellt.

+0

Die übergeordnete ID "11446498" ist das in der API verwendete Beispiel. Der, den ich in meinem Code verwende, ist gültig, ich habe es überprüft. – Neptune

+1

Versuchen Sie, das abschließende "/" vom URL-Endpunkt zu entfernen. Der korrekte Endpunkt ist [https://upload.box.com/api/2.0/files/content](https://docs.box.com/reference#upload-a-file) für die [Dokumentation] (https: //docs.box.com/reference#upload-a-file). Die erste Zeile des Codes sollte sein: 'String URL = "https://upload.box.com/api/2.0/files/content";' – Brent

+0

Sie Brent Dank, es ist das größte Problem war in meinem Code! – Neptune

0

Ich fand die Lösung, die verschiedenen Teile waren nicht korrekt. Ich hatte drei Teile zu erstellen:

  1. PARENT_ID: die ID des übergeordneten Ordner
  2. Metadaten: die json
  3. Datei: Die Datei

Dieser Code zum Hochladen funktioniert:

String URL = "https://upload.box.com/api/2.0/files/content"; 
    HttpClient httpClient = new HttpClient(); 
    PostMethod postMethod = new PostMethod(URL); 
    postMethod.setRequestHeader("Authorization", "Bearer "+ this.token); 

    try { 
     List<Part> parts = new ArrayList<Part>(); 

     parts.add(new StringPart("parent_id", parentId)); 

     JSONObject parent = new JSONObject(); 
     parent.put("id", this.parentId); 

     JSONObject attributes = new JSONObject(); 
     attributes.put("parent", parent); 
     attributes.put("name", file.getName()); 

     StringPart strPart = new StringPart("metadata", attributes.toString()); 
     strPart.setContentType("text/plain"); 
     parts.add(strPart); 

     ByteArrayPartSource source = new ByteArrayPartSource(file.getName(), 
       IOUtils.toByteArray(this.file)); 
     parts.add(new FilePart("file", source)); 

     postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams())); 
     httpClient.executeMethod(postMethod); 

     // checks server's status code first 
     int status = postMethod.getStatusCode(); 
     System.out.println(status); 
     if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) { 
      String jsonText = postMethod.getResponseBodyAsString(); 
      JSONObject json = new JSONObject(jsonText); 
      System.out.println(jsonText); 
     } else { 
      throw new MyException(postMethod.getResponseBodyAsString()); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     postMethod.releaseConnection(); 
    } 
Verwandte Themen