Java Servlet File Upload of images - is the code efficient or allowing for memory leak? -


my servlet following file submitted upload through jsp page.

step 1 - file, image
step 2 - check if needs resized
step 2a - if needs resize,
step 3 - resize , create thumbnail size
step 4 - store new resized image in folder on server
step 5 - store thumbnail image in folder on server

the largest image size (per client's request) servlet accept processing 3900 x 3900...i know, huge! that's client wants.

i've put servlet on tomcat on vps testing , i'm seeing pretty serious memory consumption.

vps memory limits
i 316mb of memory. fresh restart on tomcat, memory @ 108mb. vps running apache server.
apache, tomcat stopped using 45mb, tomcat taking 63mb on initial startup.

tomcat.conf - file has following set heap.

# initial java heap size (in mb) wrapper.java.initmemory=100  # maximum java heap size (in mb) wrapper.java.maxmemory=128 

once run image upload process - submitting image file through form on jsp page - servlet takes on there doing steps described above.

i use same image test things out. image sized @ 3872 x 2592 pixels , equates 2.53mb.

i submit image 5 times. each upload takes minute, or more complete.

the test results

1 image : memory=150mb
2 image : memory = 179mb
3 image : 179.8
4 image: 188.8
5 image: 189.3

why such jump after uploading image 2.53mb? don't it. when memory drops, drops slightly. uploaded image after first 5, , memory went 188.8.

is there inefficient code? made sure close streams, or @ least i'm pretty sure did.

any input appreciated.



my image upload , resizing servlet

public void addphoto(statement stmt, httpservletrequest req, httpservletresponse res, httpsession session) {              bufferedinputstream bis = null;              bufferedimage bufimg = null;             bufferedimage resized = null;             bytearrayoutputstream bytestream = null;             bufferedoutputstream bos = null;             fileoutputstream fos = null;      try {              string imagemain = "";             string imagethumb = "";                              string dbshortpathmain = "images/gallery/";                          string dbshortpaththumb = "images/gallery/";                          string filenametolowercase = "";        string fileextension = "";         byte[] newimage = null;            boolean ispart = servletfileupload.ismultipartcontent(req);         if(ispart) {            fileitemfactory factory = new diskfileitemfactory();           servletfileupload fileupload = new servletfileupload(factory);           java.util.list items = fileupload.parserequest(req);           iterator = items.iterator();           while(it.hasnext()) { //while1                                                  fileitem item = (fileitem)it.next();                     string fieldvalue = item.getname();                   if(!item.isformfield()) {                      string fieldname = item.getfieldname();                                                                   if(fieldname.equals("imagemain")) { //10                            //--------------------------                           //continue processing upload                           //field wasn't blank                           //---------------------------                           // create fileitem object access file.                           file f = new file(fieldvalue);                             // content type filename.                           string contenttype = getservletcontext().getmimetype(f.getname());                                                               if(contenttype == null) contenttype = "none";                      if(!contenttype.startswith("image")) {                        appendtourl += "&notfile=y";                                                    } else {                           //check size                                                       bis = new bufferedinputstream(item.getinputstream());                                                     //bufferedimage                             bufimg = imageio.read(bis);                                                                       //check size of image                              int img_width = bufimg.getwidth();                             int img_height = bufimg.getheight();                              //not accepting images larger 3900 x 3900                             if(img_width > 3900 || img_height > 3900) {                                 appendtourl += "&filesize=y";                                                              } else {                                                            //------------------------------------------                               //  r e s z e  &   u p l o d   f l e                                //------------------------------------------                                 //#### step 1 - make size (600 max width) image ####                                  double scale = (double)maxlargeimagesize/(double)img_width;                                                               image sized = getscaledinstanceawt(bufimg, scale, image.scale_smooth);                                  //convert image bufferedimage                                 resized = tobufferedimage(sized, bufferedimage.type_int_rgb);                                                                     bytestream = new bytearrayoutputstream();                                         //make file name characters lowercase, , extract extension (.jpg)                                 filenametolowercase = fieldvalue.tolowercase();                                                      fileextension = filenametolowercase.substring(filenametolowercase.indexof(".")+1,filenametolowercase.length());                                  //initialize buffer output stream efficiency                                                  //bufferedoutputstream                                 bos = new bufferedoutputstream(bytestream);                                  //w r t e image bufferedoutputstream , bytearrayoutputstream                                 if(fileextension.equals("png"))                                     imageio.write(resized,"png",bos);                                                               if(fileextension.equals("jpg"))                                                 imageio.write(resized,"jpg",bos);                                                               if(fileextension.equals("jpeg"))                                                    imageio.write(resized,"jpeg",bos);                                                              if(fileextension.equals("gif"))                                    imageio.write(resized,"gif",bos);                                          if(fileextension.equals("bmp"))                                    imageio.write(resized,"bmp",bos);                                                                     // flush bytearrayoutputstream                                                               bytestream.flush();                                           //convert bytes in stream byte array                                 //byte[]                                 newimage = bytestream.tobytearray();                                                                                                              //specify file name                                  imagemain = fileuploadpath+"/thenewfile."+fileextension;                                 dbshortpathmain += cat+"/thenewfile."+fileextension;                                  fos = new fileoutputstream(imagemain);                                                fos.write(newimage);                                  fos.flush();                                                                      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                  //#### step 2 - make thumb size (140 max width) image ####                                 scale = (double)maxthumbimagesize/(double)img_height;                                                                 sized = getscaledinstanceawt(bufimg, scale, image.scale_smooth);                                  //convert image bufferedimage                                 resized = tobufferedimage(sized, bufferedimage.type_int_rgb);                                  //used convert new image bytes can put db                                 //bytearrayoutputstream                                 bytestream = new bytearrayoutputstream();                                                         //initialize buffer output stream efficiency                                                  //bufferedoutputstream                                 bos = new bufferedoutputstream(bytestream);                                  //w r t e image bufferedoutputstream , bytearrayoutputstream                                 if(fileextension.equals("png"))                                     imageio.write(resized,"png",bos);                                                               if(fileextension.equals("jpg"))                                                 imageio.write(resized,"jpg",bos);                                                               if(fileextension.equals("jpeg"))                                                    imageio.write(resized,"jpeg",bos);                                                              if(fileextension.equals("gif"))                                    imageio.write(resized,"gif",bos);                                          if(fileextension.equals("bmp"))                                    imageio.write(resized,"bmp",bos);                                                                     // flush bytearrayoutputstream                                                               bytestream.flush();                                           //convert bytes in stream byte array                                 //byte[]                                 newimage = bytestream.tobytearray();                                                                                                              //specify file name                                 imagethumb = fileuploadpath+"/newfilethumb."+fileextension;                                 dbshortpaththumb += cat+"/newfilethumb."+fileextension;                                  fos = new fileoutputstream(imagethumb);                                               fos.write(newimage);                                  fos.flush();                                                                 //#### end large size ####                                  //---------------------------------------                          }                          }                                              }                                               }//isformfield                    }//while                    }//ispart      }//try     catch(exception e) {}       {     if (bis != null) try { bis.close(); } catch (ioexception logorignore) {}     if (bytestream != null) try { bytestream.close(); } catch (ioexception logorignore) {}     if (bos != null) try { bos.close(); } catch (ioexception logorignore) {}     if (fos != null) try { fos.close(); } catch (ioexception logorignore) {}     }         }//addphoto   //----------------------------------------------------------- //r e s z n g   m g e   m e t h o d s //-----------------------------------------------------------         public bufferedimage tobufferedimage(image image, int type) {             bufferedimage result = null;             try {                     int w = image.getwidth(null);                     int h = image.getheight(null);                     result = new bufferedimage(w, h, type);                     graphics2d g = result.creategraphics();                     g.drawimage(image, 0, 0, null);                     g.dispose();                                     }//try                 catch(exception e) {}                  return result;         }//end          public image getscaledinstanceawt(bufferedimage source, double scale, int hint) {                     image newimage = null;             try {                                int w = (int) (source.getwidth() * scale);                     int h = (int) (source.getheight() * scale);                             newimage = source.getscaledinstance(w, h, hint);                 }//try                 catch(exception e) {}                                 return newimage;         }//end 


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -