Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete file

Status
Not open for further replies.

blevy9

Programmer
Apr 22, 2003
16
US
I need to implement code in my JSP page to delete file from certain catalog if file exists; File name is a String variable. How to do it?
 
I know forward only path to the directory, where file located, and file name to delete must be a String variable, which is changed at run time.
 


Code:
String s = "myfile.txt";
File f = new File(s);
f.delete();
if (f.exists()) {
   throw new IOException("Deletiion failed");
}

What do you mean by "forward directory" ?
 
File descriptor includes full pathname:
File(String pathname)
For example, for absolute path name:
c:\Program Files\Sybase\EAServer\rpt\report1.pdf
how we can create a new File object in our jsp page?
What separator we should use for Windows : "/" or "\" ?
In different books I see different information about it.
 
Hi,

Try to execute this to find out the absolute path names. This should help you...

public class AbsoluteRelativePath {

private static void doConversion() {

// Create a File object
File fileName = new File("README_InputFile.txt");
System.out.println();
System.out.println("Original Filename = " + fileName + "\n");

fileName = fileName.getAbsoluteFile();
System.out.println("Absolute Filename = " + fileName + "\n");

fileName = new File("dir" + File.separatorChar + "README_InputFile.txt");
fileName = fileName.getAbsoluteFile();
System.out.println("Absolute Filename with \"dir\" = " + fileName + "\n");

fileName = new File(".." + File.separatorChar + "README_InputFile.txt");
fileName = fileName.getAbsoluteFile();
System.out.println("Absolute Filename with \"..\" = " + fileName + "\n");

}

public static void main(String[] args) {
doConversion();
}

}

Cheers,
Venu
 
I know exactly what absolute path is on my local drive, so I use it in my JSP code :

<%@ page import=&quot;java.io.*&quot; %>
<%
String ls_path = &quot;c:\\program files\\sybase\\easerver 4.1.1\\Repository\\WebApplication\\my_jspclient\\&quot;;
String s_full;
String s_dataset = request.getParameter( &quot;datasetname&quot; );
s_full = ls_path + s_dataset;
File f = new File(s_full);
if (f.exists()) {
f.delete();
}
%>

I print full name (s_full) to check it and see that it is correct.
I don't get errors, but file does not remove from directory.

Instead of absolute pass I tried to use URL (which we using to run WEB Application) as argument for File object, :
but it does not work too - no errors, but file does not remove.
 
are you sure that &quot; ls_path + s_dataset&quot; resolves to a proper file name, and that you have permissions to write (and so delete) the file. Are you sure that no other processes are using or accessing that file. The File object throws no exceptions, so your problem must be one of the following :

the file does not exist
the file is writeable
another process is using the file.
 
I found, that I have problem when I try to delete File after redirection in my JSP page to Client browser (this is PDF file, so I need to use redirection):
response.sendRedirect(ls_filename);
( I have error message in Server log file: Static Resource not found)

If I simply use (for HTML file):
out.println(ls_filename);
File deletion works fine after this.

Something prevent File deletion after redirection.
May be I need to clean response header (status code) after redirection?
 
So you are doing a sendRedirect to a pdf, and then trying to delete it ? This will not work if this is the case because the browser is using the file ...

 
It is correct, I am doing sendRedirect to a pdf, and then try to find a way to delete pdf file from the server. I understand now, it does not work because of Client Browser is using the file after redirection.
But I need to find out how to delete this file from the server after redirection, as database includes hundreds of files, and if files will not be removed, they will occupy all memory on the server.
I want to try to use onUnload JavaScript event, onUnload handler executed just before the page is unloaded, which occurs when the browser is about to move on to a new page.
But, as the first, I don't know if I am able from JavaScript event handler call JSP script to delete the file;
the second, when onUnload handler is executed, browser could still using redirected file, and the object could not be garbage collected.
Any idea how to delete redirected file from the server will be appreciated.
 
Ho are you cerating the pdf's - if they come from a BLOB in the database, then you could extract them and push them to the browser without writing them to the file system.

Is this possible for you ?

 
I am n't using blob, I am using EAServer and PowerBuilder components, deployed to EAServer. So I should use redirection.
If it is not possible to delete the redirected file, I am interested if it is possible use File object to delete all pdf files, generated in the certain directory on the server? I would be able to create separate Java program to delete all PDF files from the server. But File object does not support &quot;*&quot; to delete all files, only particulat file:
File f = new File(&quot;*.pdf&quot;);
f.delete();
- it will not work
 
OK, there is another much better way of doing this.

Basically, you have a servlet which handles the deletion and also loading of your pdf file.

When the jsp page loads, it embeds the pdf in the <embed> tag by calling the servlet with the required &quot;file&quot; parameter.

Then when the page is closed, or unloaded, it calls the servlet with the &quot;del&quot; parameter to delete the file.


Code:
<html>
<% String file = request.getParameter(&quot;file&quot;);
%>

<head>
 <script language=&quot;javascript&quot;>
   function del() {
       var file = '<%=file%>;
       document.location=&quot;servlet/FileServlet?del=&quot;+file;
    }
  </script>
</head>
<body onunload=&quot;del()&quot;>
<embed src=&quot;servlet/FileServlet?file=<%=file%>&quot; type=&quot;application/pdf&quot; width=&quot;100%&quot; height=&quot;100%&quot;>
<body>
</html>


Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FileServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			doPost(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       String del = request.getParameter(&quot;del&quot;);
       if (del != null);
           new File(del).delete();
           return;
       }
   
	String file = request.getParameter(&quot;file&quot;);
	response.setContentType(&quot;application/pdf&quot;);
	
	ServletOutputStream out = response.getOutputStream();
	
	File f = new File(file);
	FileInputStream fis = new FileInputStream(f);
	bytes = new byte[(int)f.length()];
	fis.read(bytes, 0, bytes.length);
	
	out.write(bytes);
	
    }
    
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top