*
The moose likes Servlets and the fly likes file download is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "file download is not working" Watch "file download is not working" New topic
Author

file download is not working

anna malai
Greenhorn

Joined: May 07, 2005
Posts: 1
I am having a file in the server which is to be downloaded.
for that one link with the file name is provided. on cliking the link the following code is called.


ServletOutputStream sos=null;
response.setHeader("Content-Disposition","attachment; filename="+st/*.substring(temp+1)*/);
response.setContentType("application/download");
FileInputStream bufReader = new FileInputStream(request.getRealPath(st));
int line=bufReader.read();
sos = response.getOutputStream();
sos.flush();
while(line!=-1){
sos.write(line);
line=bufReader.read();
}
bufReader.close();
sos.flush();

the problem is
1. when the save as dialogue pops up it displays the filename to be downloaded correctly for the first time. when save button is cliked it again asks with the name of the html page.

2. when the save button is pressed for second time it saves the html file.

3. some times an error " the page you are looking for is temporarily unavailable or removed" is displayed by the browser IE 5.0 but not in IE 6.0
both in win2000 os.

4. the same code had worked fine already.

how to solve these?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not an expert on Java streams so I'm not going to try to figure out what's wrong with your code. I will, however, give you a piece of code that's working for me.



I will mention two things that I noticed about your code.
1.) you're flushing the buffer before you've started reading into it.
I don't know if this is causing a problem but it's certainly not necessary.

2.) Your variable names (combined with the lack of UBB code tags) make it very difficult for someone else to read your code.
You're reading and writing chars as ints but naming them "line" (it's possible to read from a text file one line at a time and at first glance, I thought that's what you were doing.)
You've also named a FileInputStream "bufReader" which makes it look, at first glance, like you're using an instance of java.io.BufferedReader.

I normally don't pick nits but in this case, I think these issues are going to keep you from getting help with your code.
[ May 07, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file download is not working
 
Similar Threads
file upload sos
SOS
SOS send SMS
How to prevent file download dialog from appearing
SOS