Reputation: 116
I'm on a very strange situation. I'm running a servlet that returns an image on Tomcat.
I'm trying to run this code: FabricaControladores fabrica = FabricaControladores.getInstance();
String nombreProveedor = request.getParameter("nombre");
IVerInformacionDeProveedor infoProveedor = fabrica.getIVerInformacionDeProveedor();
infoProveedor.SeleccionarProveedor(nombreProveedor);
DataProveedor data = infoProveedor.VerInformacionProveedor();
image = data.getImagen();
BufferedImage bImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bImage.createGraphics();
graphics.drawImage(image,0,0,null);
graphics.dispose();
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
ImageIO.write(bImage, "jpeg", out);
out.close();
And when i run it i get this error:
HTTP Status 500 - Width (-1) and height (-1) cannot be <= 0
type Exception report
message Width (-1) and height (-1) cannot be <= 0
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
java.awt.image.BufferedImage.<init>(BufferedImage.java:331)
MostrarImagen.doGet(MostrarImagen.java:110)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.
Apache Tomcat/7.0.42
what is very strange is that using a very similar method to get the image inside a SWING application it works without problem.
And when I load the image directly using:
new ImageIcon(getClass().getResource("/Imagens/TC.jpg")).getImage())
it shows it perfectly.
Is like the image is lost or corrupted somewhere. Any ideas?
EDIT
DATA PROVEEDOR
public class DataProveedor{
private String _nickname, _nombreCompania, _nombre, _apellido, _correo, _paginaWeb;
Image _imagen;
private Date _fechaNacimiento;
private Hashtable<Integer,DataProducto> _productos;
public DataProveedor(String nickname, String nombreCompania,
String nombre, String apellido, String correo,
String paginaWeb, Date fechaNacimiento,
Hashtable<Integer, DataProducto> productos) {
this._nickname = nickname;
this._nombreCompania = nombreCompania;
this._nombre = nombre;
this._apellido = apellido;
this._correo = correo;
this._imagen = null;
this._paginaWeb = paginaWeb;
this._fechaNacimiento = fechaNacimiento;
this._productos = productos;
}
public DataProveedor(String nickname,String nombreCompania,
String nombre, String apellido, String correo,
String paginaWeb, Date fechaNacimiento,
Hashtable<Integer, DataProducto> productos, Image imagen) {
this._nickname = nickname;
this._nombreCompania = nombreCompania;
this._nombre = nombre;
this._apellido = apellido;
this._correo = correo;
this._imagen = imagen;
this._paginaWeb = paginaWeb;
this._fechaNacimiento = fechaNacimiento;
this._productos = productos;
}
public String getNickname() {
return _nickname;
}
public String getNombreCompania() {
return _nombreCompania;
}
public String getNombre() {
return _nombre;
}
public String getApellido() {
return _apellido;
}
public String getCorreo() {
return _correo;
}
public Image getImagen() {
return _imagen;
}
public String getPaginaWeb() {
return _paginaWeb;
}
public Date getFechaNacimiento() {
return _fechaNacimiento;
}
public Hashtable<Integer, DataProducto> getProductos() {
return _productos;
}
** VER INFO **
@Override
public DataProveedor VerInformacionProveedor() {
return _proveedor.GetDataProveedor();
}
** GetDataProveedor **
public DataProveedor GetDataProveedor()
{
Hashtable<Integer, DataProducto>productos = new Hashtable<Integer, DataProducto>();
for(Producto p : _productos.values())
{
DataProducto ip = p.GetInfo();
productos.put(new Integer(ip.get_id()), ip);
}
DataProveedor dp;
if(this._imagen == null)
dp = new DataProveedor(GetNickname(),GetNombreCompania(),GetNombre(),GetApellido(),
GetCorreo(),GetPaginaWeb(),GetFechaNacimiento(),productos);
else
dp = new DataProveedor(GetNickname(),GetNombreCompania(),GetNombre(),GetApellido(),GetCorreo(),
GetPaginaWeb(),GetFechaNacimiento(),productos,this._imagen.getImagen());
return dp;
}
** DataProveedor **
public DataProveedor(String nickname, String nombreCompania,
String nombre, String apellido, String correo,
String paginaWeb, Date fechaNacimiento,
Hashtable<Integer, DataProducto> productos) {
this._nickname = nickname;
this._nombreCompania = nombreCompania;
this._nombre = nombre;
this._apellido = apellido;
this._correo = correo;
this._imagen = null;
this._paginaWeb = paginaWeb;
this._fechaNacimiento = fechaNacimiento;
this._productos = productos;
}
public DataProveedor(String nickname,String nombreCompania,
String nombre, String apellido, String correo,
String paginaWeb, Date fechaNacimiento,
Hashtable<Integer, DataProducto> productos, Image imagen) {
this._nickname = nickname;
this._nombreCompania = nombreCompania;
this._nombre = nombre;
this._apellido = apellido;
this._correo = correo;
this._imagen = imagen;
this._paginaWeb = paginaWeb;
this._fechaNacimiento = fechaNacimiento;
this._productos = productos;
}
Upvotes: 0
Views: 124
Reputation: 27094
The line containing
new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
fails because one or both of image.getWidth(null)
and image.getHeight(null)
returns -1
.
I don't know exactly where image
is coming from, but it seems to be an instance of java.awt.Image
. These images are asynchronous, and intended for Applet or other GUI application use. The problem you are encountering, is because the asynchronous loading of the image has not yet happened, and the dimension of the image is still unknown.
The reason why the code works using image = new ImageIcon(...).getImage()
is because the ImageIcon
constructor has code that makes sure the image is fully loaded before returning.
For a server-side application, you will make life a lot easier for yourself, by using a BufferedImage
. You can read a BufferedImage
using ImageIO.read(...)
.
Upvotes: 1