Julio Francisco Veronelli ha escrito:
en web.xml, agregas un pattern para mapear las url, de por ejemplo, :
htttp://tuAplicacion/images (es decir, /images/*)
y las mandas a un Servlet
ahí mismo podes parsear el URL, por ejemplo yo mando en la url el id
que identifica la imagen:
private void doAction(HttpServletRequest req, HttpServletResponse
resp) throws ServletException, IOException {
//Obtenes la URL
String url = req.getRequestURL().toString();
//Parseo...
String nameFile = url.substring(url.lastIndexOf("/")+1);
String codigo = nameFile.substring(0,nameFile.indexOf("."));
int idImagen = Integer.parseInt(codigo);
//Ejemplo... aca la obtenes de donde la tengas
MiImagen imagen= Aplicacion.getInstance.getImagenes.getImagen(idImagen);
//Importantisimo, aca seteas la salida. Tipicamente "image/jpeg" o "image/gif"
//Busca mime types en google para mas info.
resp.setContentType(imagen.getContentType());
//Escribis en la salida
byte[] image = imagen.getBytes();
resp.getOutputStream().write(image);
}
Espero te sirva...
Retoque rapido mi código para el ejemplo, no se si se me paso algo
pero la idea es que te sirva de guía y creo te puede ayudar.
Saludos!
Sebastián
byte[] fotoB = usuarioLocal.getFotoUsu(idUsu);
ByteArrayOutputStream output = new ByteArrayOutputStream();
output.write(fotoB, 0, fotoB.length);
response.setContentType("image/jpeg ");
response.setHeader("content-disposition",
"inline; fileName=\"foto.jpg\"");
response.setContentLength(output.size());
//Escribir el archivo en el response
OutputStream out = response.getOutputStream();
output.writeTo(out);
out.flush();
out.close();
La jsp que mostrará la foto simplemente hace referencia al servlet (
en este caso Action) recien mostrado:
<img src="usuarioAction.do?hacia=getFotoUsu&idUsu=<%=
usuario.getIdUsu()%>" width="120px" height="150px" align="right"
alt="fotografía del usuario">
De esta forma, un Action(o servlet) puede entregar los datos para
cargar la JSP con información y otro Action solamente entrega la
imagen.
(nunca pensé que el src del tag img podía referirse a un Action, pero
funciona a la perfeccion)