File Download:
~~~~~~~~~~~~~~
>response.setHeader("content-type","application/vnd.ms-excel")I have 2 questions: 1) I am trying to figure which MIME type can be used for all file types (.xml, .html, .dat, etc). Is there any? 2) Also is there a way to force downloading of all file types, instead of letting the web browser open them. PS: Joe, thanks for the information, I really appreciate the help. Regards, Daniel -----Original Message----- From: Joe Germuska [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 19, 2002 9:56 PM To: Daniel J. D'Cotta Subject: RE: File Upload/Download Issues At 10:48 AM +0800 2002/06/19, Daniel J. D'Cotta wrote: >Joe, > >Thanks... but I have a question. (Posting to you direct because it is not >Struts related) > >>>3. Finally, How do you let users download a file with Struts? The file >>>cannot be outside WEB-INF. >> >>You'd need to write directly to the HttpServletResponse object and >>return a null ActionMapping from the perform/execute method of your >>Action. You're responsible for setting all the headers (content >>type, etc). But this isn't hard to do -- it's just not something >>that Struts helps you do. > >How would I do this? well, ServletResponse (the superclass of HttpServletResponse) has two methods, "getOutputStream()" and "getWriter()", each of which return classes to which you can write data (getOutputStream returns a ServletOutputStream, and getWriter returns a java.io.PrintWriter). So, once your action decided that everything was OK, first, it would do some setup on the response object -- * set a status code: if everything is OK, you'd call "response.setStatus(HttpServletResponse.SC_OK)" The full list of status codes is in the api doc for HttpServletResponse. * set a MIME type -- for example, for an Excel spreadsheet: 'setHeader("content-type","application/vnd.ms-excel");' The official list of registered MIME types is at <http://www.isi.edu/in-notes/iana/assignments/media-types/media-types>, although it may be easier to just look at your own web browsers "helper applications" settings. * get a handle on your content -- if it's in "WEB-INF", the easiest way is to use ServletContext.getResourceAsStream("/WEB-INF/path/to/file"), although I believe WebLogic 6 has problems with this (I know Weblogic 6 can't load JSPs from WEB-INF, but not sure about regular files) * copy from your content InputStream to the ServletOutputStream (or wrap them in reader/writer classes, etc -- standard java.io stuff here). * close the streams and you should be done. It's been a while since I've spit content directly out at the response -- obviously it's something you only want to do when you need to. There may be other HTTP headers that you need to set, at least in some cases, etc. I like the O'Reilly "Webmaster in a Nutshell" for a quick reference to HTTP headers and the details of that protocol, although I'm sure you can find comparable material online. Hope that helps. Joe -- -- * Joe Germuska { [EMAIL PROTECTED] } "It's pitiful, sometimes, if they've got it bad. Their eyes get glazed, they go white, their hands tremble.... As I watch them I often feel that a dope peddler is a gentleman compared with the man who sells records." --Sam Goody, 1956 tune in posse radio: <http://www.live365.com/stations/289268> ___________________________________________________________________________ To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
