bonjour comme le titre indique, j'ai un petit soucie pour faire l'up load d'un fichier avec struts voila mes code:
index.jsp (contien le formulaire)
le bean (UploadActionForm)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <html:form action="/index.do?from=sender" method="post" enctype="multipart/form-data"> <html:file property="fichier"/> <br><html:submit value="submit"/> </html:form>
le action bean(UploadAction)
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 package com.myapp.struts; import org.apache.struts.upload.FormFile; public class UploadActionForm extends org.apache.struts.action.ActionForm { protected FormFile fichier; public FormFile getFichier() { return fichier; } public void setFichier(FormFile fichier) { this.fichier = fichier; } public UploadActionForm() { super(); } }
stuts-config.xml
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 package com.myapp.struts; import java.io.File; import java.io.FileOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.*; import org.apache.struts.upload.FormFile; /** * * @author Administrateur */ public class UploadAction extends org.apache.struts.action.Action { /* forward name="success" path="" */ private final static String SUCCESS = "success"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadActionForm myForm = (UploadActionForm)form; // Process the FormFile FormFile myFile = myForm.getFichier(); String contentType = myFile.getContentType(); //Get the file name String fileName = myFile.getFileName(); //int fileSize = myFile.getFileSize(); byte[] fileData = myFile.getFileData(); //Get the servers upload directory real path name String filePath = getServlet().getServletContext().getRealPath("/") +"upload"; /* Save file on the server */ if(!fileName.equals("")){ System.out.println("Server path:" +filePath); //Create file File fileToCreate = new File(filePath, fileName); //If file does not exists create file if(!fileToCreate.exists()){ FileOutputStream fileOutStream = new FileOutputStream(fileToCreate); fileOutStream.write(myFile.getFileData()); fileOutStream.flush(); fileOutStream.close(); } } //Set file name to the request object request.setAttribute("fileName",fileName); return mapping.findForward(SUCCESS); } }
success.jsp( la page qui affiche le lien pour télécharger le fichier(just pour tester l'upload)
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 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="UploadActionForm" type="com.myapp.struts.UploadActionForm"/> </form-beans> <global-exceptions> </global-exceptions> <global-forwards> <forward name="welcome" path="/Welcome.do"/> </global-forwards> <action-mappings> <action name="UploadActionForm" path="/index" scope="request" type="com.myapp.struts.UploadAction" validate="false"> <forward name="success" path="/WEB-INF/success.jsp"/> </action> <action path="/Welcome" forward="/welcomeStruts.jsp"/> </action-mappings> <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/> <message-resources parameter="com/myapp/struts/ApplicationResource"/> <plug-in className="org.apache.struts.tiles.TilesPlugin" > <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> <set-property property="moduleAware" value="true" /> </plug-in> <!-- ========================= Validator plugin ================================= --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> </struts-config>
et voila les erreur afficher par tom le chat
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 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <% String fileName=(String)request.getAttribute("fileName"); %> <p align="center"><font size="5" color="#000080">File Successfully Received</font></p> <p align="center"><a href="upload/<%=fileName%>">Click here to download</a></p> </body> </html>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 java.io.FileNotFoundException: C:\Documents and Settings\Administrateur\Mes documents\NetBeansProjects\upload\build\web\upload\Introduction à Hibernate.mht (Le chemin d'accès spécifié est introuvable) java.io.FileOutputStream.open(Native Method) java.io.FileOutputStream.(FileOutputStream.java:179) java.io.FileOutputStream.(FileOutputStream.java:131) com.myapp.struts.UploadAction.execute(UploadAction.java:50) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
Partager