Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Downloading file from server

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am trying to write a servlet which will return a file from the server. I have tried it but every time the file is opened in the browser. I want the file to be sent to the client such that he has the option to save locally or open it from current location (like normal downloads). please advice.
Nikhil
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You can use Mime type in your
setContentType line.
Let me know whether this one works or not.
Regards
Kumar
------------------
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
There is a couple of steps to this process:
1.) Set the setContentType and setHeader methods in the response area, for example:
response.setContentType("application/x-filler");
response.setHeader("Content-Disposition","attachment;filename="+ fileName;
2.)Open a ServletOutputStream and write the file to it using a BufferedInputStream.
Let me know if you need additional information.
Joe
 
Trader Joey
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
thanks joe,
i tried out the code as you suggested !! i do get the download option window with save and open options. the problem now is that the file name is not correctly displayed. the filename displayed is something like this : the name of the servlet + ( some code ). i will keeping trying. thanks a ton for your help.
nikhil
 
Joe Paolangeli
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The filename displayed in the download dialog box should be the filename specified in the response.setHeader method.
Good Luck,
Joe
 
Trader Joey
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I GOT IT !!! its working fine now.. thanks a lots once again to joe and ramesh
Nikhil
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why my code in not working in Windows platform?
This code perfectly working in Linux Netscape only
response.setContentType("application/x-filler"); response.setHeader("Content-Disposition","attachment;filename=test.txt");
OutputStream outStream = response.getOutputStream();
FileInputStream fileInput = new FileInputStream(fileName);
int numOfBytes = fileInput.available();
byte byteArray[] = new byte[numOfBytes];
int nextByte = fileInput.read(byteArray);
fileInput.close();
OutputStream outStream = response.getOutputStream();
outStream.write(byteArray);
outStream.close();
Thanks in advance
[This message has been edited by ram mohan (edited January 14, 2001).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Nikhil,
its good to hear that your code for downloading file works. Does it succeed in dowloading any file type like image etc. or only text formats? Please clarify.
KB
 
Trader Joey
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Kruger Brent,
well the servlet download any kind of file not just txt files.
Nikhil
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
I used the same codes specified above, On windows, I got a small pop up window and allow me to choose the folder from my local drive on both IE and Netscape. But on Mac, this will save the file directly into my desktop without the pop up selection box.
I want to make this code work on Mac too. Is there something to do with the browser's setting or.....please advise
Thanks
Heather
------------------

[This message has been edited by Heather Wu (edited April 23, 2001).]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Nikhil Nikhil:
thanks joe,
i tried out the code as you suggested !! i do get the download option window with save and open options. the problem now is that the file name is not correctly displayed. the filename displayed is something like this : the name of the servlet + ( some code ). i will keeping trying. thanks a ton for your help.
nikhil


I am having a similar problem debugging code written by someone else.
The user clicks a button to download a file. The button executes a servlet. The servlet opens the response's output stream. Then we set the response content type (it is being set to "application/x-myapp-txt" (where myapp is the name of my application). I spoke to the original developer about why he made up a MIME type and he said it was to force the Save As dialog to display. Then we set the Content-Disposition in the response header to "attachment;filename=myfile.txt" where myfile.txt is a name the servlet has constructed based on the data being downloaded. Next the data is written to the output stream and the output stream is flushed and closed.
We are running WebLogic as our web server.
On most PCs clicking the button display the File Download screen, letting us chose Save to display the Save As dialog. On the Save As dialog the filename we specified is display in the file field and the Save as Type dropdown shows Text Document and All Files. The data is saved in the file correctly.
On one user's PC we have a problem. In this case when the Save As dialog pops up, the filename display is the name of the servlet that was invoked to do the download (with an HTML extension). The Save as Type dropdown shows Html Document and All Files. If the user saves the file, what gets saved is the HTML for the page where the download button is located.
We thought maybe that our download servlet isn't sending the correct information. So, in the servlet we commented out the lines that set the content type and content-disposition. The expected and hoped for result is that the data that should be downloaded is instead sent to the browser and displayed as text. Indeed it is.
We suspect it might be a browser or Windows setting. We've checked the browser and all the settings seem to be the same as on other machines that work.
The questions are:
What causes IE to display an incorrect name for one user when it works for all other users?
What causes the incorrect data to be saved to a file (even though we know the correct data can be sent to the browser)?
 
mark weitz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by mark weitz:

The questions are:
What causes IE to display an incorrect name for one user when it works for all other users?
What causes the incorrect data to be saved to a file (even though we know the correct data can be sent to the browser)?



After a bit of research on the Microsoft IE site, I have found this link that explains that this is a bug with certain versions of IE (specifically IE 5.5 with service pack 1)
http://support.microsoft.com/support/kb/articles/Q281/1/19.ASP?LN=EN-US&SD=gn&FR=0&qry=file%20download%20saves%20as%20html&rnk=8&src=DHCS_MSPSS_gn_SRCH&SPR=IE
Thanks anyway!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hey Nikhil,
I am having the same problem that the saveAs Dialog is displaying the name of servlet instead of displaying name of file I am downloading. Can you tell me what you did for SaveAs dialog to display correct file name ??
Thanks.
Abba

Originally posted by Nikhil Nikhil:
thanks joe,
i tried out the code as you suggested !! i do get the download option window with save and open options. the problem now is that the file name is not correctly displayed. the filename displayed is something like this : the name of the servlet + ( some code ). i will keeping trying. thanks a ton for your help.
nikhil


 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi,
I followed the code present by ram, it works. I use this code to download PDF file. The thing is it only work in IE not in Netscape. Netscape always open PDF in a seperate window, just don't save to local machine. How could I solve it?
Jim
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi guys,
i have got a similar problem with downloading rtf
file.
I was reading an rtf file from local system(servlet system) and it was downloading fine in browser with correct name with save as dialog ofcourse my browser has rtf plugin.
But I have changed the code now as getting the file as a string from my ejb and writing the string to the out with setContentType and setContentHeader as it is.but now it directly opens in browser.
But if the file is procured by reading from system by servlet,then the save as dialog does not have problem.
I tried using servletoutputstream.write(bytearray) also after conevrting the string into bytearray using getBytes.it doesn't work .Any ideas please.
regards
madhu
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi guys,
I've really just got enough of IE's incidental misbehavior with file downloading. There are already far a bucketful of discussion threads 'round here that go round and round in circles with this topic with no definite solution in sight. Just make a search and see what you'll find
I've just finished an Intranet application using JSP/Servlet/JavaBeans (MVC) that a part of its function is to return a file from the server. I was stuck with making all sorts of workarounds to at least make the behavior satisfactory in IE because majority of my users use IE 4.0 (and yes, weren't we told of making our Web apps cater to majority of the browsers?!). In the end I have to make do with the ff:

I won't tell the grisly details, but this is at least good enough with IE 4.0, 5.0, and 5.5. And with Netscape...
For IE 5.5, I hit a gold mine that

works perfectly OK
But I'm not stopping here As a resort, I'm now working on a solution that instead of having the browser download the file, I'm letting an applet do it. I can say that the applet takes the place of the browser. My applet connects to a servlet (hmmm, with HTTP/firewall tunneling, I guess), which in turn retrieves the file and streams it to the applet. In order for the applet to save the file to the user's disk drive, the applet should be signed.
With that I think I could sleep soundly now. I just couldn't let myself (and Java) be simply swept aside by some stubborn browser created by some &@^*$%#! company.
Ex Animo Java!
-- Val
    Bookmark Topic Watch Topic
  • New Topic