Bonjour,
j'ai un petit problème que je ne comprends pas...
J'ai regarder des tutos, des posts similaires, pourtant je ne vois pas ou est mon erreur.
Je veux uploader un fichier avec <html:file>
UploadForm.java :
Struts-config :
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
42
43
44
45
46 package form; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; @SuppressWarnings("serial") public class UploadForm extends ActionForm { private FormFile fileName; private String typLoad = "album"; private String albumParent; private boolean herit; public FormFile getFileName() { return fileName; } public void setFileName(FormFile fileName) { this.fileName = fileName; } public String getTypLoad() { return typLoad; } public void setTypLoad(String typLoad) { this.typLoad = typLoad; } public String getAlbumParent() { return albumParent; } public void setAlbumParent(String albumParent) { this.albumParent = albumParent; } public boolean isHerit() { return herit; } public void setHerit(boolean herit) { this.herit = herit; } }
UploadInitAction.java:
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 <form-bean name="uploadForm" type="form.UploadForm"/> ... <action path="/UploadInit" input="/Pages/Albums.jsp" type="action.UploadInitAction"> <forward name="succes" path="/Albums.do"/> </action> <action path="/Upload" name="uploadForm" input="/Pages/Albums.jsp" type="action.UploadAction"> <forward name="succes" path="/UploadInit.do"/> </action>
UploadAction.java:
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 package action; public class UploadInitAction extends Action{ /** * Method execute * @param ActionMapping mapping * @param ActionForm form * @param HttpServletRequest request * @param HttpServletResponse response * @return ActionForward * @throws Exception */ @SuppressWarnings("unchecked") public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession httpsession = request.getSession(); String pseudo = (String)httpsession.getAttribute("login"); Session session = HibernateUtil.currentSession(); Query query1 = ... query1.setParameter(...); List<...> mesAlbums = (List<...>)query1.list(); request.setAttribute("albums", mesAlbums); return mapping.findForward("succes"); } }
ma jsp:
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
42
43
44
45
46
47
48 package action; public class UploadAction extends Action{ /** * Method execute * @param ActionMapping mapping * @param ActionForm form * @param HttpServletRequest request * @param HttpServletResponse response * @return ActionForward * @throws Exception */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //Traitement de l'upload @SuppressWarnings("unused") UploadForm monForm = (UploadForm) form; FormFile myFile = monForm.getFileName(); /*String contentType = myFile.getContentType(); //recuperer le nom du fichier String fileName = myFile.getFileName(); int fileSize = myFile.getFileSize(); byte[] fileData = myFile.getFileData(); //ca permet de retourne le chemin où sera sauvegarder le fichier String filePath = getServlet().getServletContext().getRealPath("/") +"upload"; /* Sauvegarde du fichier dans le serveur if(!fileName.equals("")){ System.out.println("Serveur path:" +filePath); //creer le fichier File fileToCreate = new File(filePath, fileName); //si le fichier n'existe pas, il faut le sauvegarder if(!fileToCreate.exists()){ FileOutputStream fileOutStream = new FileOutputStream(fileToCreate); fileOutStream.write(myFile.getFileData()); fileOutStream.flush(); fileOutStream.close(); } }*/ return mapping.findForward("succes"); } }
Et quand je valide le formulaires j'ai ces erreurs :
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 <%@ include file="/Pages/Includes/include.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <script type="text/javascript"> function changevisibility(c,v){ obj1 = document.getElementById(c); obj2 = document.getElementById(v); if (obj1.style.display == 'block'){ obj1.style.display = 'none'; obj2.style.display = 'block'; }else{ obj1.style.display = 'none'; obj2.style.display = 'block'; } if (obj2 == document.getElementById('cssAlbum_albumBlock')){ document.getElementById('cssAlbum_imgLoad').style.height = "190px"; }else{ document.getElementById('cssAlbum_imgLoad').style.height = "210px"; document.forms["uploadForm"].elements["herit"].checked = false; document.forms["uploadForm"].elements["albumParent"].disabled = true; document.forms["uploadForm"].elements["albumParent"].selectedIndex = 0; } } function checkrepalbum(){ obj=document.forms["uploadForm"].elements["herit"]; obj2=document.forms["uploadForm"].elements["albumParent"]; if (obj.checked == true){obj2.disabled = false;} else{obj2.disabled = true;} } </script> <body> <html:form action="/Upload" method="post" enctype="multipart/form-data"> <div id="cssAlbum_imgLoad"> <img width="100%" height="100%" src="<%=request.getContextPath()%>/Images/inscription_fondWL.png"/> <div class="cssAlbum_load"> <div class="cssAlbum_choixLoad"> <div class="cssAlbum_radio"><html:radio property="typLoad" value="album" onclick="changevisibility('cssAlbum_photoBlock','cssAlbum_albumBlock')"/><bean:message key="albums.choixType.typAlbum"/></div> <div class="cssAlbum_radio"><html:radio property="typLoad" value="photo" onclick="changevisibility('cssAlbum_albumBlock','cssAlbum_photoBlock')"/><bean:message key="albums.choixType.typImg"/></div> </div> <div id="cssAlbum_albumBlock"> <div class="cssAlbum_loadAlbumIN"> <div class="cssAlbum_loadAlbumLabel"><bean:message key="albums.label.repertoire"/></div> <div class="cssAlbum_loadAlbumFile"><html:file property="fileName" size="60px"/></div> </div> </div> <div id="cssAlbum_photoBlock"> <div class="cssAlbum_loadPhotoIN"> <div class="cssAlbum_photoLabel"> <html:checkbox property="herit" onclick="checkrepalbum()" ></html:checkbox> <bean:message key="albums.label.album"/> <html:select name="uploadForm" property="albumParent" disabled="true" > <html:option value="NULL"><bean:message key="albums.choixalbum"/></html:option> <html:optionsCollection name="albums" value="nom" label="nom" /> </html:select> </div> <div class="cssAlbum_loadPhotoLabel"><bean:message key="albums.label.repertoire"/></div> <div class="cssAlbum_loadPhotoFile"><html:file property="fileName" size="60px"/></div> </div> </div> <html:submit value="Upload"/> </div> </div> </html:form> </body> </html>
je sais plus quoi faire, je vois pas pourquoi il me parle d'incompatibilité entre ArrayList et FormFile (je n'est meme pas d'arrayList dans FormBean). Peut etre avec la liste que je fais passer dans mon request dans uploadInitAction mais je vois pas ou est le rapport.19 nov. 2009 19:06:15 org.apache.struts.chain.commands.AbstractExceptionHandler execute
ATTENTION: Unhandled exception
javax.servlet.ServletException: BeanUtils.populate
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:475)
at org.apache.struts.chain.commands.servlet.PopulateActionForm.populate(PopulateActionForm.java:50)
at org.apache.struts.chain.commands.AbstractPopulateActionForm.execute(AbstractPopulateActionForm.java:60)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Cannot invoke form.UploadForm.setFileName on bean class 'class form.UploadForm' - argument type mismatch - had objects of type "java.util.ArrayList" but expected signature "org.apache.struts.upload.FormFile"
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2181)
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2141)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:433)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:473)
... 23 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2155)
... 30 more
Merci d'avance.
Partager