Upload fichier avec la Class Part
Bonjour
Je tente de uploader une image depuis une page jsp
Lors de ma requête voici le msg d'erreur que j'obtient
message org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
Je ne parviens pas à règler mon problème, une personne peut elle m'aider svp ?
Voici le code que j'ai mis en place
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
|
MA JSP
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Envoi de fichier</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="inc/form.css">
</head>
<body>
<form action="upload" method="post" enctype="multipart/for-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}" />
<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" name="form_fichier" class="sansLabel"/>
</fieldset>
</form>
</body>
</html> |
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 77 78 79 80 81 82 83 84 85 86 87 88 89
|
MA SERVLET
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
/**
* Servlet implementation class Upload
*/
@WebServlet("/upload")
@MultipartConfig (location = "/tmp", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
public class Upload extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String VUE = "upload.jsp";
public static final String CHAMP_DESCRIPTION = "description";
public static final String CHAMP_FICHIER = "fichier";
/**
* @see HttpServlet#HttpServlet()
*/
public Upload() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
req.getRequestDispatcher(VUE).forward(req, resp);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String description = req.getParameter(CHAMP_DESCRIPTION);
req.setAttribute(CHAMP_DESCRIPTION, description);
/*
* Les données reçu sont multiPart
* on doit donc utiliser la methode getPart() pour trater le champ fichier
*/
Part part = req.getPart(CHAMP_FICHIER);
String nomFichier = getNomFichier(part);
if (nomFichier != null && !nomFichier.isEmpty()) {
String nomChamp = part.getName();
req.setAttribute(nomChamp, nomFichier);
}
req.getRequestDispatcher(VUE).forward(req, resp);
}
private static String getNomFichier(Part part) {
/*
* On vérifie dans l'entete que l'attribut filename existe
* Boucle sur chacun des parametre
*/
for(String contentDisposition : part.getHeader("Content-Disposition").split(";")) {
//recherche du parametre filename
if(contentDisposition.trim().startsWith("filename")) {
/*
* revoi du nom du fichier
*/
return contentDisposition.substring(contentDisposition.indexOf('=')+ 1);
}
}
return null;
}
} |