Bonjour, j'ai quelques soucis avec l'utilisation de l'api multipart.
Tout d’abord voici l'architecture du message post que j'envoie:
Ce qui donne:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 // Instanciation du POST HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("host"); // Creation d'un POST multipart MultipartEntity multipartEntity = new MultipartEntity(); try { // Architecture d'un part FormBodyPart identifiantPart = new FormBodyPart("identifiant", new StringBody("value1")); identifiantPart.addField("Content-ID", "value2"); FormBodyPart servicePart = new FormBodyPart("service", new StringBody("value3")); serviceIDPart.addField("Content-ID", "value4"); // Ajout du part au POST multipartEntity.addPart(identifiantPart); multipartEntity.addPart(servicePart); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } // Ajout du multiPart dans le post httpPost.setEntity(multipartEntity); // Envoie du Post et Reception de la reponse try { HttpResponse httpResponse = httpClient.execute(httpPost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
J'utlise l'api fileupload pour récupéré les informations depuis une servlet. Voici mon code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) --pJoYnSbnMr1aGq8HRWN4AT4KkL07ErYnn 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-Disposition: form-data; name="identifiant" 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-Type: text/plain; charset=US-ASCII 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-Transfer-Encoding: 8bit 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-ID: value2 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) value1 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) --pJoYnSbnMr1aGq8HRWN4AT4KkL07ErYnn 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-Disposition: form-data; name="service" 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-Type: text/plain; charset=US-ASCII 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-Transfer-Encoding: 8bit 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) Content-ID: value4 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) 11:29:33,331 INFO [stdout] (http--0.0.0.0-8080-2) value3 11:29:33,346 INFO [stdout] (http--0.0.0.0-8080-2) --pJoYnSbnMr1aGq8HRWN4AT4KkL07ErYnn--
Je récupère bien mon champ principal mais je n'arrive pas récupérer le champ (Content-ID) que j'ajoute dans mes parts. Avez vous une idée de comment je pourrais faire en utilisant cet api? merci d'avance
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { System.out.println("request is multipart"); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); // Parse the request FileItemIterator iter; try { iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); System.out.println("toString : " + item.toString()); InputStream stream = item.openStream(); if (item.isFormField()) { System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected."); } else { System.out.println("File field " + name + " with file name " + item.getName() + " detected."); // Process the input stream } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Partager