Problème de visualisation d'image par servlet après Upload (.JPG pas pris en compte)
Bonjour ;
Voila dans mon petit formulaire , quand j'upload une image de type PNG ou GIF et que je me rend dans ma page de 'list_client.jsp' et je clique sur le bouton voir , l'image s'affiche correctement toute seule ! comme j'ai bien indiqué dans ma servlet 'Image.java' (qui gère la visualisation des images) ou j'ai mis inline dans l’entête de setHeader pour juste voir l'image du client :
Code:
response.setHeader( "Content-Disposition", "inline; filename=\"" + fichier.getName() + "\"" );
Mais quand je choisis la forme JPG , et je clique sur le lien hypertexte voir la photo se télécharge (sous chrome) a la place de juste visualisé , c'est comme si j'ai mis un attachment au lieu de inline dans l’entête de setHeader :
Code:
response.setHeader( "Content-Disposition", "attachment; filename=\"" + fichier.getName() + "\"" );
Je ne sais pas si la format JPG est pris en compte par le serveur Tomcat 7 ou est ce que je dois faire des modification quelque part , bref voila mon code de Image.java ou je traitre les entête http pour visualiser mes photos et le tableau des clients list_client.jsp ou j'ai mis le lien voir :
==> Image.java :
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 90 91 92 93 94 95 96 97 98 99 100 101 102
| @WebServlet( "/Image" )
public class Image extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String CHEMIN = "chemin";
private static final int TAILLE_TAMPON = 10240; // 10ko
public Image() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
IOException {
String chemin = this.getServletConfig().getInitParameter( CHEMIN );
/*
* Récupération du chemin du fichier demandé au sein de l'URL de la
* requête
*/
String fichierRequis = request.getPathInfo();
/* Vérifie qu'un fichier a bien été fourni */
if ( fichierRequis == null || "/".equals( fichierRequis ) ) {
/*
* Si non, alors on envoie une erreur 404, qui signifie que la
* ressource demandée n'existe pas
*/
response.sendError( HttpServletResponse.SC_NOT_FOUND );
return;
}
/*
* Décode le nom de fichier récupéré, susceptible de contenir des
* espaces et autres caractères spéciaux, et prépare l'objet File
*/
fichierRequis = URLDecoder.decode( fichierRequis, "UTF-8" );
File fichier = new File( chemin, fichierRequis );
/* Vérifie que le fichier existe bien */
if ( !fichier.exists() ) {
/*
* Si non, alors on envoie une erreur 404, qui signifie que la
* ressource demandée n'existe pas
*/
response.sendError( HttpServletResponse.SC_NOT_FOUND );
return;
}
/* Récupère le type du fichier */
String type = getServletContext().getMimeType( fichier.getName() );
/*
* Si le type de fichier est inconnu, alors on initialise un type par
* défaut
*/
if ( type == null ) {
type = "application/octet-stream";
}
/* Initialise la réponse HTTP */
response.reset();
response.setBufferSize( TAILLE_TAMPON );
response.setContentType( type );
response.setHeader( "Content-Length", String.valueOf( fichier.length() ) );
// attachment : pour télécharger
// inline : pour voir une image dans une page après le clique
response.setHeader( "Content-Disposition", "inline; filename=\"" + fichier.getName() + "\"" );
/* Prépare les flux */
BufferedInputStream entree = null;
BufferedOutputStream sortie = null;
try {
/* Ouvre les flux */
entree = new BufferedInputStream( new FileInputStream( fichier ), TAILLE_TAMPON );
sortie = new BufferedOutputStream( response.getOutputStream(), TAILLE_TAMPON );
/* Lit le fichier et écrit son contenu dans la réponse HTTP */
byte[] tampon = new byte[TAILLE_TAMPON];
int longueur;
while ( ( longueur = entree.read( tampon ) ) > 0 ) {
sortie.write( tampon, 0, longueur );
}
} finally {
try {
sortie.close();
} catch ( IOException ignore ) {
}
try {
entree.close();
} catch ( IOException ignore ) {
}
}
}
} |
list_client.jsp :
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
| <div id="tableaClient">
<c:choose>
<c:when test="${empty sessionScope.sessionClient}">
<c:out value="Aucun client n'est valable"></c:out>
</c:when>
<c:otherwise>
<table border="1">
<tr>
<th>Id</th>
<th>Nom du client</th>
<th>Prénom du client</th>
<th>Adresse</th>
<th>Telephone</th>
<th>Email</th>
<th>Image</th>
<th>Action</th>
</tr>
<c:forEach items="${sessionScope.sessionClient}" var="mapClient">
<tr class="${boucle.index % 2 == 0 ? 'pair' : 'impair'}">
<td>${mapClient.value.id_client}</td>
<td>${mapClient.value.nom}</td>
<td>${mapClient.value.prenom}</td>
<td>${mapClient.value.adresse_livraison}</td>
<td>${mapClient.value.telephone}</td>
<td>${mapClient.value.email}</td>
<td>
*****<%-- On ne construit et affiche un lien vers l'image que si elle existe. --%>
******<c:if test="${ !empty mapClient.value.image }">
******<c:set var="image"><c:out value="${ mapClient.value.image }"/></c:set>
******<a href="<c:url value="/images/${ image }"/>">Voir</a>
******</c:if>
****</td>
<td> <a href="<c:url value="/SupprimerClient"><c:param name="idKeyClient" value="${mapClient.key}"/></c:url>">Supprimer</a> </td>
</tr>
</c:forEach>
</table>
</c:otherwise>
</c:choose>
</div> |
==> web.xml :
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
| <?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:jsp="http://java.sun.com/xml/ns/javaee/jsp" 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>ClientInscription</servlet-name>
<servlet-class>com.commande.servlets.ClientInscription</servlet-class>
<init-param>
<param-name>chemin</param-name>
<param-value>/client_image/images/</param-value>
</init-param>
<multipart-config>
<location>C:/client_image/images</location>
<max-file-size>1048576</max-file-size>
<max-request-size>52428800</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>ClientInscription</servlet-name>
<url-pattern>/clientInscription</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ListClient</servlet-name>
<servlet-class>com.commande.servlets.ListClient</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListClient</servlet-name>
<url-pattern>/listClient</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Image</servlet-name>
<servlet-class>com.commande.servlets.Image</servlet-class>
<init-param>
<param-name>chemin</param-name>
<param-value>/client_image/images/</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Image</servlet-name>
<url-pattern>/images/*</url-pattern>
</servlet-mapping>
</web-app> |
Merci