java - How to upload a file to a specific folder in struts 2 -


i want upload file struts action. need in action path folder:

i tried using

string contextpath = request.getcontextpath(); 

but i'm getting java.lang.nullpointerexception

either store in catalina parent folder project folder

      string rootpath = system.getproperty("catalina.home");       file dir = new file(rootpath + file.separator + "yourfoldername");       if (!dir.exists())           dir.mkdirs();        // create file on server       java.util.date date= new java.util.date();       string path = dir.getabsolutepath() + file.separator + (new timestamp(date.gettime())).tostring().replace(":", "").tostring().replace(".", ".").tostring().replace(" ","").tostring().replace("-","").tostring()+".pdf"; 

or make folder in project , store there.

if (!file.isempty()) {     //filter checking file extewnsion     if(file.getcontenttype().equalsignorecase("image/jpg") || file.getcontenttype().equalsignorecase("image/jpeg")){         //if file >2 mb or < 2mb         double size = file.getsize();         double kilobytes = (size / 1024);         double megabytes = (kilobytes / 1024);         if(megabytes<2){     try {         byte[] bytes = file.getbytes();         string filepath = request.getrealpath("/")+"yourfoldername\\profileimages\\"+sessionmanagement.getusername()+".jpg";         bufferedoutputstream stream =                 new bufferedoutputstream(new fileoutputstream(new file(filepath)));         stream.write(bytes);         stream.close();          //console call     }     else{         model.put("error", "please select file less 2 mb");         return new modelandview("uploadphototile");     }     }else{         model.put("error", "please select jpeg file");         return new modelandview("uploadphototile");     } } else {     model.put("error", "please select file");     return new modelandview("uploadphototile"); } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -