Servlet/JSP affichage d'une image
Bonjour,
Pour une application distribuée je desire envoyer une image sur le serveur Tomcat avec une jsp et afficher cette image sur la meme jsp.
Avez vous un idée pour faire ceci svp?
Pour info jutilise L'api fileUpload commons dans ma jsp.
voici le code de ma servlet qui affiche une image stockée sur le serveur dans le naviguateur.
DisplayImageServlet.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
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
HttpSession session = req.getSession();
ServletContext sc = getServletContext();
String filename = sc.getRealPath("1-30.jpg");
// Get the MIME type of the image
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+filename);
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
// Set content type
//resp.setContentType(mimeType);
// Set content size
File file = new File(filename);
resp.setContentLength((int)file.length());
// Open the file and output streams
FileInputStream in = new FileInputStream(file);
//OutputStream ou = resp.getOutputStream();
// Copy the contents of the file to the output stream
resp.setContentType("image/jpeg" );
BufferedOutputStream out2 = new BufferedOutputStream(resp.getOutputStream());
byte by[] = new byte[ 32768 ];
int index = in.read( by, 0, 32768 );
while ( index != -1 )
{
out2.write( by, 0, index );
index = in.read( by, 0, 32768 );
}
out2.flush();
} |
Merci d'avance!