Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to delete file

 
manishkumar Mishra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am not being able to delete the file. I am getting the Exception as:

"java.lang.IllegalStateException: OutputStream already obtained".

As I know we can't obtain OutputStream second time but I need to delete the file after executing these code.

My Code in Jsp:-

fileBytes = objSessionBean.getBytesFromFile(new File(szInputFile));
if(fileBytes != null){
response.reset();
response.setContentType("application/vnd.ms-excel;charset=euc-kr");
response.setHeader("Content-Disposition","attachment; filename=\""+szFile+"\"");
outs = response.getOutputStream();
outs.write(fileBytes);
outs.flush();
outs.close();

objSessionBean.deleteFile(szInputFile); //Call to my EJB method to delete file

}

-----------
In EJB class I am simply calling "delete" of File Class to delete the file.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the EJB method to delete the file do ?

What does this have to do with JSPs ?
 
manishkumar Mishra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually there is no any signification of EJB. I just mean that in my architecture the above posted codes are written in JSP from where I am giving a call to an method to delete the file which is written in EJB.


My Code written in EJB Bean class is :

public void deleteFile(String fileName)
{
File f = new File(fileName);
if(!f.exists())
throw new IllegalArgumentException("Delete: no such file or directory: " + fileName);
if(!f.canWrite())
throw new IllegalArgumentException("Delete: write protected: " + fileName);
if(f.isDirectory())
{
String files[] = f.list();
if(files.length > 0)
throw new IllegalArgumentException("Delete: directory not empty: " + fileName);
}

boolean success = f.delete(); // Here its throwing the exception

if(!success)
throw new IllegalArgumentException("Delete: deletion failed");
else
return;
}
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only exception that delete() throws is a SecurityException.


JSP from where I am giving a call to an method to delete the file which is written in EJB.


:shocked: the view is not supposed to do that. But thats another thing altogether

Please post the entire stack trace. The bean method also has too many IllegalArgumentExceptions being thrown from it. It would be neater to simply return true or false based on whether the operation succeeded or not
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are assuming that the problem is a result of the delete. Has it occurred to you that the problem may be your misuse of the servlet response output stream?

What does the stack trace of the error indicate?
 
manishkumar Mishra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for yor suggestion.After reading your suggestion I tried to analyse my code and found that the problem was not with delete method.

In my code one of the FileOutputStream object was not closed which was open in an another method..probably that is why it was not allowing to delete the file..
When I closed that object,the file is getting deleted now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!