Upload Request mittels Jersey und Multipart

mrdustinhoffmann

Neues Mitglied
Hallo zusammen,

ich hänge jetzt schon einige Tage an einem Projekt und hoffe, dass man mich hier ein Stückchen weiter bringt.
Eine eingesetzter Virenscanner bietet eine JSON-API Schnittstelle an, die ich gerne ansprechen möchte. Reine JSON Requests funktioniert, nun möchte ich aber auch eine Datei mit hochladen und da hapert es noch.

Laut Dokumentation gibt es folgendes Request-Example:

Code:
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="request";
Content-Type: application/json
{
    "request": {
        "md5": "da855ff838250f45d528a5a05692f14e",
        "file_name": "MyFile.docx",
        "file_type": "docx",
        "features": ["te"],
        "te": {
            "reports": ["pdf", "xml"],
            "images": [
                {
                    "id": "7e6fe36e-889e-4c25-8704-56378f0830df",
                    "revision": 1
                },
                {
                    "id": "e50e99f3-5963-4573-af9e-e3f4750b55e2",
                    "revision": 1
                }
            ]
        }
    }
}

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="MyFile.docx"
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

[Binary content of MyFile.docx]

----WebKitFormBoundary7MA4YWxkTrZu0gW

Meine Anfrage sieht wie folgt aus:
Code:
--Boundary_1_1073533248_1495615540079
Content-Type: application/json
Content-Disposition: form-data; name="request"

{"request":{"md5":"e6ab04aeef4d5502d56a05f63a27312e","file_name":"Test1.txt","file_type":"docx"}}
--Boundary_1_1073533248_1495615540079
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="Test1.txt"; name="file"

Irgendein Text

--Boundary_1_1073533248_1495615540079--

Im Vergleich zum Beispiel heißt mein Boundary anders und der Content-Type wird anders definiert. Da ich in diesem Bereich nur wenig Erfahrung habe: Spielt das eine Rolle?

Die Antwort des Servers:
Code:
{
   "response" : [
      {
         "status" : {
            "code" : 1008,
            "label" : "BAD_REQUEST",
            "message" : "Invalid Multipart/form request"
         }
      }
   ]
}

Hier noch mein Quellcode:
Code:
    private void exampleMultiPartUpload(File fileToUpload) throws Exception {
        final String API_URI = "https://IP:PORT/tecloud/api/v1/file/upload";

        final ClientConfig config = new DefaultClientConfig();
        config.getClasses().add(JacksonJsonProvider.class);
        config.getClasses().add(MultiPartWriter.class);
        config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        final Client client = Client.create(config);
        client.addFilter(new LoggingFilter(System.out));
        final WebResource resource = client.resource(API_URI);

       
       
        //MD5 berechnen:
        String digest = MD5Checksum.getMD5Checksum(fileToUpload.getAbsolutePath());
       
        // the file to upload, represented as FileDataBodyPart
        FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
                fileToUpload,
                MediaType.APPLICATION_OCTET_STREAM_TYPE);
        fileDataBodyPart.setContentDisposition(
                FormDataContentDisposition.name("file")
                        .fileName(fileToUpload.getName()).build());
  
        // some json to send to the server as an element of the multi part request
        final JSONObject jsonToSend = new JSONObject();
        final JSONObject data_file = new JSONObject();
       
        data_file.put("md5", digest);
        data_file.put("file_name", fileToUpload.getName());
        data_file.put("file_type", "docx");
        jsonToSend.put("request", data_file);

        /* create the MultiPartRequest with:
         * Text field called "description"
         * JSON field called "characterProfile"
         * Text field called "filename"
         * Binary body part called "file" using fileDataBodyPart
         */
        final MultiPart multiPart = new FormDataMultiPart()
                .field("request", jsonToSend, MediaType.APPLICATION_JSON_TYPE)
                .bodyPart(fileDataBodyPart);
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

        // POST request final
        ClientResponse response = resource
                .type("multipart/form-data").post(ClientResponse.class,
                        multiPart);
       
        final String result = getStringFromInputStream(response.getEntityInputStream());
        System.out.println("INFO >>> Response from API was: " + result);
        client.destroy();
    }


Falls noch Informationen fehlen, bitte kurze Info. :)
Und danke schon mal an alle, die sich die Mühe machen, sich das Thema anzuschauen.


Viele Grüße
Dustin
 

Ähnliche Java Themen

Neue Themen


Oben