If I insert the name of the image into the database but it does not save the image in the path I give it, I get the following error:
Exception: the request does not contain a multipart / form-data or multipart / form-data stream, content type header is nullException: the request does not contain a multipart / form-data or multipart / form-data stream, content type header is nullorg.apache.commons.fileupload.disk.DiskFileItemFactory@7641070d
<form action="uploadfile" method="GET" enctype="multipart/form-data">
<table>
<tr>
<td>
<input type="text" id="file_name" name="nombre">
<input type="file" name="file" id="file" value="Seleccione Imagen"/>
<input type="submit" value="SUBIR ARCHIVO"/>
</td>
</tr>
</table>
</form>
and I use javascript to separate a false route that gives me and I just select the name of the image:
$(function(){
$("#file").change(function(){
var name = $("#file").val();
var arreglo = name.split("\");
console.log(arreglo);
var image_name = arreglo[2];
$("#file_name").val(image_name);
});
});
in the servlet I have the following code:
try{
ConectaDB conect = new ConectaDB();
Connection c = conect.conectar();
String archivo = request.getParameter("file");
System.out.println(archivo);
String nombre = "venta";
String img_producto= "imagenes/"+archivo;
int id_categoria=2;
float precio = 2200;
int stock = 10;
String query = "INSERT INTO productos(nombre,img_producto,id_categoria, precio,stock) VALUES ('"+nombre+"','"+img_producto+"','"+id_categoria+"','"+precio+"','"+stock+"');";
Statement stm = c.createStatement();
stm.executeUpdate(query);
stm.close();
c.close();
System.out.println(query);
}catch(SQLException sqlx) {
sqlx.printStackTrace();
}
String archivourl = /home/luis/Escritorio/prueba/web/imagenes";
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024);
factory.setRepository(new File(archivourl));
ServletFileUpload upload = new ServletFileUpload(factory);
try{
List<FileItem> partes = upload.parseRequest(request);
for(FileItem items: partes){
File file = new File(archivourl,items.getName());
items.write(file);
}
response.sendRedirect("index.jsp");
out.print("<h2>ARCHIVO CORRECTAMENTE SUBIDO.....</h2>"+"\n\n"+"<a href='index.jsp'>VOVLER AL MENU</a>");
}catch(Exception e){
out.print("Exception: "+e.getMessage()+"");
}
}