Problème récupération du fichier depuis son formulaire
Salut ,,
Voila , j'ai un formulaire upload.jsp ou j'entre la description dans un champ text et j'ai un autre champ file , ou je met mon fichier , je veux après avoir cliquer sur le bouton Envoyer , afficher devant chaque champ le nom que j'ai déja ecris dans le champ descripton et le nom du fichier que j'ai uploader , sauf que ca me donne cette erreur : (je suis sur Apache Tomcat 7.0.41 , APi servlet 3.0)
java.lang.NullPointerException
com.fichiers.servlets.Upload.getNomFichier(Upload.java:85)
com.fichiers.servlets.Upload.doPost(Upload.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Dans la console :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| "Servlet.service()" pour la servlet com.fichiers.servlets.Upload a généré une exception
java.lang.NullPointerException
at com.fichiers.servlets.Upload.getNomFichier(Upload.java:85)
at com.fichiers.servlets.Upload.doPost(Upload.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source) |
L'erreur de la console m'indique cette ligne (85) dans ma servlet Upload:
Code:
for ( String contentDisposition : part.getHeader( "Content-Disposition" ).split( ";" ) ) {
Voila ma servlet Upload :
Code:
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
| public class Upload extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String vue_upload = "/WEB-INF/upload.jsp";
public static final String CHAMP_DESCRIPTION = "description";
public static final String CHAMP_FICHIER = "fichier";
public Upload() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
IOException {
this.getServletContext().getRequestDispatcher( vue_upload ).forward( request, response );
}
protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
IOException {
/* Récupération du contenu du champ de description */
String description = request.getParameter( CHAMP_DESCRIPTION );
request.setAttribute( CHAMP_DESCRIPTION, description );
/*
* Les données reçues sont multipart, on doit donc utiliser la méthode
* getPart() pour traiter le champ d'envoi de fichiers.
*/
Part part = request.getPart( CHAMP_FICHIER );
/*
* Il faut déterminer s'il s'agit d'un champ classique ou d'un champ de
* type fichier : on délègue cette opération à la méthode utilitaire
* getNomFichier().
*/
String nomFichier = getNomFichier( part );
/*
* Si la méthode a renvoyé quelque chose, il s'agit donc d'un champ de
* type fichier (input type="file").
*/
if ( nomFichier != null && !nomFichier.isEmpty() ) {
String nomChamp = part.getName();
request.setAttribute( nomChamp, nomFichier );
}
this.getServletContext().getRequestDispatcher( vue_upload ).forward( request, response );
}
/*
* Méthode utilitaire qui a pour unique but d'analyser l'en-tête
* "content-disposition", et de vérifier si le paramètre "filename" y est
* présent. Si oui, alors le champ traité est de type File et la méthode
* retourne son nom, sinon il s'agit d'un champ de formulaire classique et
* la méthode retourne null.
*/
private static String getNomFichier( Part part ) {
/* Boucle sur chacun des paramètres de l'en-tête "content-disposition". */
for ( String contentDisposition : part.getHeader( "Content-Disposition" ).split( ";" ) ) {
/* Recherche de l'éventuelle présence du paramètre "filename". */
if ( contentDisposition.trim().startsWith( "filename" ) ) {
/*
* Si "filename" est présent, alors renvoi de sa valeur,
* c'est-à-dire du nom de fichier.
*/
return contentDisposition.substring( contentDisposition.indexOf( '=' ) + 1 );
}
}
/* Et pour terminer, si rien n'a été trouvé... */
return null;
}
} |
Mon formulaire dans la page jsp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <form action="<c:url value="/Upload" />" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Envoi de fichier</legend>
<label for="description">Description du fichier</label>
<input type="text" id="description" name="description" value="" />
<span class="succes"><c:out value="${description}" /></span>
<br />
<label for="fichier">Emplacement du fichier <span class="requis">*</span></label>
<input type="file" id="fichier" name="fichier" />
<span class="succes"><c:out value="${fichier}" /></span>
<br />
<input type="submit" value="Envoyer" class="sansLabel" />
<br />
</fieldset>
</form> |
Et enfin le web.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>com.fichiers.servlets.Upload</servlet-class>
<multipart-config>
<location>C:\fichiers</location>
<max-file-size>10485760</max-file-size> <!-- 10 Mo -->
<max-request-size>52428800</max-request-size> <!-- 5 x 10 Mo -->
<file-size-threshold>1048576</file-size-threshold> <!-- 1 Mo -->
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
</web-app> |
Et merci !