I've had nighmares about this since 3 days ago and couldn't find anything by googling so here I am.
As the title says so i need to find a way to store and then retrieve images from a mysql database I have tired the code below to do so (the storing part) but it just saves empty blobs.
I'd apreciate the help and since i haven't seen much about the topic so will a lot of people
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
String description = null;
MyImage image= new MyImage();
try
{
//Download com.oreilly package
MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);
Enumeration files=multi.getFileNames();
File file=null;
for(int i = 0 ; files.hasMoreElements() ; i++)
{
String name=(String)files.nextElement();
String filename=multi.getFilesystemName(name);
String type=multi.getContentType(name);
file=multi.getFile(name);
description = multi.getParameter("description");
out.println("The File is "+archivo.getCanonicalPath() + "<br>");
BufferedImage img = ImageIO.read(file);
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ImageIO.write(img, "pnm", bas);
byte[] data = bas.toByteArray();
if(i == 0){
image.setLoginImage(data);
}
if(i == 1){
image.setReportImage(data);
}
}
image.setDescription(description);
String error = validateFields();
if(error.equals("")){
try{
EntityManager em = MyEntityManager.getEntityManager();
em.getTransaction().begin();
em.persist(image);
em.getTransaction().commit();
}catch(Exception e){
session.setAttribute("Error", e.getMessage());
}
}else{
session.setAttribute("Error", error);
}
}catch(Exception e){
session.setAttribute("Error", e.getMessage());
}
%>
</body>
</html>