SetBinaryStream - GetBinaryStream

Soraya
17 de Diciembre del 2003
Hola chic@s tengo un problema urgente.

Tengo una base de datos y dos servlets uno para subir ficheros (Long raw) a la base de datos y otro para descargarlos. (Mediante setbinarystream y getbinarystream).

El problema es que cuando los sube los sube bien, pero al decirle en el servlet que me los muestre, me los baja con el nombre del servlet que los crea, en mi caso ExtraerFileServlet, y entonces las imagenes y los jpg u otro tipo de documentos no me los muestra bien. En cambio, los de texto si. Sabeis por que???

Aqui esta el codigo del ExtraerFileServlet, a ver si se os ocurre por que.

------------Base de Datos SQL-----------------------
create table image (
id number constraint pk_image primary key,
unitid number,
format number,
image long raw
)

----------ExtraerFileServlet.java-----------

public class ExtraerFileServlet extends HttpServlet {

ConexionBd cbd ;
private int idf;
int resultado = 0;

//Inicializamos el Servlet

public void init(ServletConfig config) throws ServletException {
super.init(config);
try{
cbd = new ConexionBd();
}
catch(Exception e){
Exception re = new Exception(e.getMessage());
}
}

public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String is = req.getParameter("idfichero");
String formato = req.getParameter("formato");
System.out.println("formato" + formato);
System.out.println("idfichero" + is);
if (is!= null){
ServletOutputStream out = resp.getOutputStream();
Integer I = new Integer(is);
resp.setContentType(formato); //da igual lo que ponga saca lo que le da la gana
getImageForWeb(I.intValue(),out);
}
else{
throw new SQLException("Error al encontrar el fichero");
}
}catch (Exception sql) {
System.out.println(sql.getMessage());
}
}
public synchronized void getImageForWeb(int idfichero, ServletOutputStream out) {
String sql = "Select fichero from DOCUMENTO where Id_Fichero=" + idfichero;
try {
Statement stmt = cbd.conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if ( rs.next()) {
InputStream is;
is = rs.getBinaryStream(1);
byte[] buffer = new byte[4096];
int i= 0;
while ( ( i = is.read(buffer,0,buffer.length) ) != -1 ) {
out.write(buffer,0,i);
}
is.close();
}
rs.close();
stmt.close();
}
catch (Exception e){
System.out.println(e.getMessage());
}
}

/* Descripcion del servelt
*/
public String getServletInfo() {
return "Lee un fichero de la base de datos y lo imprime como un binary Streams";
}
}