java - Inseting/Updating image to SQL Server database by reading from a directory -
i trying insert/update image(or blob) table , column has data type image in sql server 2012. source file directory has images , named according primary key of table. need read files selecting directory , bulk insert/update table respective rows. if possible please suggest example in primefaces dialog box select directory.
my code single file upload :
private byte[] resizeimage(byte[] imgbytes) { bufferedimage originalimage = null; try { bytearrayinputstream = new bytearrayinputstream(imgbytes); system.out.println("file size before resize "+is.available()); originalimage = imageio.read(is); } catch (ioexception e) { e.printstacktrace(); } int type = originalimage.gettype() == 0 ? bufferedimage.type_int_argb : originalimage.gettype(); bufferedimage resizedimage = new bufferedimage(192, 192, type);//set width , height of image graphics2d g = resizedimage.creategraphics(); g.drawimage(originalimage, 0, 0, 192, 192, null); g.dispose(); // bufferedimage bytearrayinputstream bytearrayoutputstream os = new bytearrayoutputstream(); try { imageio.write(resizedimage, "jpg", os); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("file size after resize "+os.size()); return os.tobytearray(); }
and have :
public void upload(fileuploadevent event) { try { if (validateimage(event.getfile())) { memberimage = new defaultstreamedcontent(event.getfile().getinputstream(), "image/jpeg"); memberinformationmodel.setphoto(resizeimage(event.getfile().getcontents())); } } catch (ioexception e1) { e1.printstacktrace(); } } public boolean validateimage(uploadedfile file) { if (!(file.getcontenttype().startswith("image"))) { displayerrormessagetouser("file not image file"); //system.out.println("file not image file"); return false; } long filebyte = file.getcontents().length; if(filebyte > 102400){ //system.out.println("file size big (must @ 1mb)\n"); displayerrormessagetouser("file size big (must @ 100kb)\n"); return false; } return true; }
the above code file upload in primefaces. , saving part handled jpa/eclipselink.
Comments
Post a Comment