j'essaye de faire l'upload d'un fichier avec les RPC mais je obtenu cette erreur:
the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is text/x-gwt-rpc; charset=utf-8
-------------
My Code
-------------
1. GWT widget: FormHandle
2. Servlet:: MyFormHandlerImpl
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
35
36
37
38
39
40
41 RootPanel rootPanel = RootPanel.get(); final FormPanel form = new FormPanel(); rootPanel.add(form); form.setAction(GWT.getModuleBaseURL()+"MyFormHandler"); form.setEncoding(FormPanel.ENCODING_MULTIPART); form.setMethod(FormPanel.METHOD_POST); ......... ......... ......... panel.add(new Button("Submit", new ClickListener() { public void onClick(Widget sender) { form.submit(); } })); ......... ......... ......... form.addFormHandler(new FormHandler() { public void onSubmit(FormSubmitEvent event) { service.fileUpload(new AsyncCallback(){ public void onFailure(Throwable caught) { Window.alert("FileUpload Failure!!"); } public void onSuccess(Object result) { Window.alert("FileUpload Success!!"); }}); event.setCancelled(true); } public void onSubmitComplete(FormSubmitCompleteEvent event) { Window.alert(event.getResults()); } }); RootPanel.get().add(form)
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
35
36
37
38
39
40
41 public class MyFormHandlerImpl extends RemoteServiceServlet implements MyFormHandler { /** * */ private static final long serialVersionUID = 1L; @Override public void fileUpload() { System.out.println("0"); HttpServletRequest request = super.getThreadLocalRequest(); HttpServletResponse response = super.getThreadLocalResponse(); boolean isMultiPart = FileUpload.isMultipartContent(request);//isMultipartContent(request) String parentPath ="d:\\"; if (isMultiPart) { System.out.println("3"); DiskFileUpload upload = new DiskFileUpload(); try { List items = upload.parseRequest(request); Iterator it = items.iterator(); while (it.hasNext()) { FileItem item = (FileItem) it.next(); if(!item.isFormField()){ File fullFile = new File(item.getName()); File savedFile = new File(parentPath,fullFile.getName()); item.write(savedFile); } } } catch (FileUploadException fUE) { System.out.println("file not found"); } catch (Exception e){ System.out.println("unknown exception"); } } } }
Partager