1

I am creating a Dynamic Web Project, with Eclipse EE, using JSP and servlet. In the project a user will select a image file in the hard disk and send it to a mysql database in a LongBlob field.

This is my form to select the image file:

    <form name="formfoto1" action="guardafotonueva.jsp" method="post">
Comentario:
<input type="text" name="comentariofoto1">
<input type="file" name="archivofoto1"> 
<input type="submit" name="subir" value="Enviar foto"> 
</form>

And this is the code that don't work:

<% String cadenanombre, cadenausuarioadmin, cadenaclaveadmin;
Connection conexion = null; 
String nombrearchivofoto1, sqlfoto1, cadenacomentariofoto1, cadenacodigofoto1, cadenaarchivofoto1; 
FileInputStream fisfoto1 = null;
PreparedStatement psfoto1, pscodigof1; 
Vector<Integer> vectorcodigofotos;  
Vector<Integer> vectorcodigocliente = new Vector<Integer>(); 
ResultSet rscodigof1, rscantidadclientes;
Integer rcodigof1 = 11, cantidadclientes = 0, vn; 
String sql, sqlcodigof1; vectorcodigofotos = new Vector<Integer>();
String sqlactualizaclientesfotos, cadenaarchivo, cadenacodigotemafoto1, cadenacodigofotofoto1;
cadenacomentariofoto1 = request.getParameter("comentariofoto1"); URL url; 
cadenaarchivofoto1 = request.getParameter("archivofoto1"); File file = null;
    try
    {
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            conexion = DriverManager.getConnection("jdbc:mysql://localhost/entrenam_contenidos", "entrenam_bruno", "Bsl30121986"); 
            session.setAttribute("sessionfoto1", cadenaarchivofoto1); 
            file = new File("C://Users//Aloisio//fotos//"+cadenaarchivofoto1); 
    }
    catch(Exception ex)
    {
            out.println("Conexion sigue con problemas"); 
    }
            sqlcodigof1 = "select codigof1 from foto1 order by idf1 desc limit 1"; 
            out.println("Cadena foto: "+cadenaarchivofoto1+" su longitud es: "+cadenaarchivofoto1.length()+"</br>"); 
            pscodigof1 = conexion.prepareStatement(sqlcodigof1); 
            rscodigof1 = pscodigof1.executeQuery(); 
            while(rscodigof1.next())
            {
                rcodigof1 = rscodigof1.getInt("codigof1"); vectorcodigofotos.addElement(rcodigof1); rcodigof1++;
            } 
            session.setAttribute("codigosfotos", vectorcodigofotos); 
            session.setAttribute("cantidadfotos", vectorcodigofotos.size()); 

            sql = "insert into foto1 (codigof1, comentariof1, archivof1, blobf1) values (?, ?, ?, ?)"; 
            psfoto1 = conexion.prepareStatement(sql); 
            file = new File("C://Users//Aloisio//fotos//"+cadenaarchivofoto1); 
            fisfoto1 = new FileInputStream(file); 
            psfoto1.setInt(1, rcodigof1); 
            psfoto1.setString(2, cadenacomentariofoto1); 
            psfoto1.setString(3, cadenaarchivofoto1); 
            psfoto1.setBinaryStream(4, fisfoto1, (int) (file.length())); 
            psfoto1.execute(); 

    %>
    </body>
    </html>
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
asuarroyo
  • 11
  • 1

2 Answers2

2

try

<form name="formfoto1" action="guardafotonueva.jsp"  enctype="multipart/form-data" method="post">

instead of

<form name="formfoto1" action="guardafotonueva.jsp" method="post">
Kiran
  • 167
  • 2
  • 15
0

Try this code.

try {
MultipartHttpServletRequest multipartRequest;
    MultipartFile f;
    multipartRequest = (MultipartHttpServletRequest) request;
    f = multipartRequest.getFile("file"); 
    if(f != null) {
        long size = f.getSize(); // size of the file
        byte[] filebytes= f.getBytes() // get the file data as byte array, what you are going to store it as blob in DB
        String fileName = f.getOriginalFilename(); // Name of file
    }

    } catch (IOException e) {
    } finally {
      // do something
   }
Kiran
  • 167
  • 2
  • 15
  • Sorry! To insert this fragment of code I need to import some packege. I can't identificate the package to import. – asuarroyo Jul 21 '15 at 09:46
  • @asuarroyo please download Netbeans IDE from this [link](https://netbeans.org/downloads/). And your IDE will take care of your imports. – Kiran Aug 06 '15 at 11:51