Java - Upload file with different name -


when user select file stored in folder(path set in projectconfig file) different name entered user during uploading. here code in servlet,

new projectconfig(); // setting path storing image      string dealergallerypath = projectconfig.get("dealergallery");     diskfileitemfactory factory = new diskfileitemfactory();     int locationid=2;     servletfileupload upload = new servletfileupload(factory);     string filename = "";     try     {         java.io.file fnew = null;         //system.out.println(getservletcontext().getrealpath("/images"));          list fileitems = upload.parserequest(request);          iterator = fileitems.iterator();          while ( i.hasnext () )          {             fileitem fi = (fileitem)i.next();             if ( !fi.isformfield () )                {                 // uploaded file parameters                 string fieldname = fi.getfieldname();                 //system.out.println("field name is.."+fieldname);                 filename = fi.getname();                 if(fi.getname() != null && fi.getname().trim().length() != 0)                 {                 //system.out.println("file name is.."+filename);                  string realwebpath = dealergallerypath+locationid+"/"+fi.getname();                  fnew = new java.io.file(realwebpath); // **what here change name of file??**                 fi.write(fnew);                 system.out.println("success");                 //commonutil.setproductimage(fi.getname());                 }             }         }         system.out.println("filename::"+filename);         request.setattribute("imagename", filename);         requestdispatcher rd = request.getrequestdispatcher("/jsp/imagetesting.jsp");         rd.forward(request, response);     }catch(exception e)     {         e.printstacktrace();     } 

i had go-through on questions in site.. din't find relevant answer. thank in advance

next code works fine in website :

filepath = "upload/"; // directory in root of website. filename = fi.getname(); // original filename (you dont want it). file = new file( filepath + "newname" ); // new file new name. try { fi.write( file ); } // writes original file new file. catch ( exception ew ) { /* handle error here */ } 

your question : // what here change name of file?? well, don't change there, 2 lines above in code change "fi.getname()" new name want.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -