hi all there !
I have problem with java file object that when ever i want to delete a file from the server i have check whether its exist or not for this i get fileObject.getAbsolute path but it return me wrong path but i get path from db a give to file object although it give to error no file exist

my code the below

Thanks to in advance if not mind ?

<%
    // out.println("B4 try sk");
     ResultSet rst=null;
     ArrayList imgPath=new ArrayList();
     ArrayList imgNam=new ArrayList();
   //cUser  is current user who log on to system
 
     try{
          String query ="Select * from imageinfo where userName like'%" + cUser +"%'";
         rst=db.ExecuteQuery(query);
       }
          int count=1;
          while(rst.next())
          {
            imgPath.add(rst.getString("imagePath"));
            imgNam.add(rst.getString("imgName"));
             count++;
        }//end while loop
       
      String usrid=session.getAttribute("usr").toString();
      String serverFolder="D:\\";
      String imgID=request.getParameter("p1");
      String usrID=null;
      String location1=null;
      for(int ii=0;ii<imgPath.size();ii++)
      {
        // usrID="\\"+usrid+"\\"+"\\"+imgFold.get(ii).toString()+"\\";
          location1= imgPath.get(ii).toString()+"\\"+imgNam.get(ii).toString();
          File fi=new File(location1);
         //out.println("Cononical Path \t"+fi.getCanonicalPath());
        // File newfile=new File(fi.getCanonicalPath());
         out.println("Cononical Path \t"+fi.getPath());
          if(fi.exists())
           {
             boolean success;
             out.println("SuccessFull");
             // String fipath=fi.getAbsolutePath();
             success= fi.delete();
             out.println(success);
           }
           else
           {
             out.println(" file at  \t"+location1+"\t does not exist");
           }
         }

out put it generated is "Cononical Path C:\Documents and Settings\shahbaz\jbproject\skpiat1\Tomcat\ D:\shahbaz\Family \Eyes.jpg "
But actual path is "Path from db is D:\shahbaz\Family \Eyes.jpg "

plz plz get me out of this horrible daemon

What is the output of rst.getString("imagePath") and rst.getString("imgName") ? BTW, you shouldn't store absolute paths in a database table; consider the problems you would face when migrating your application. Use relative paths like:

image-path: shadbaz/mygallery/myfolder/
image-name: Eyes.jpg

and arrange so that your parent folder is stored as a configuration in either your database or your application. Then deleting the file would be simply:

File parent = new File(parentDirectory);
File img = new File(parent, imgPath + imgName);
if(!img.delete()) {
  //log or do something useful
}

You are getting the said output because you try to look for the image in your current working directory which in your case is the Tomcat directory.

Also, since the logic you are executing is a business scenario/use case, consider moving it to a separate class rather than put the same in JSP. Scriptlets are evil.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.