• Post Reply 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

response.setContentType("application/download"); not working

 
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello folks,

I've been trying to download a file from my server using the



My whole pertinent code is:



My file is a Microsoft Excel one, so I've also tried:




and



but no success whatsoever.

What happens in the end is that my array of bytes is shown on the browser instead of getting a dialog box asking for saving the file.

Does anyone have a clue to help me out?

Thanks in advance!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are trying to download .txt file, then use response.setHeader("Content-Type", "text/plain"); rather than response.setContentType("application/download"); .
<edit>
and why that last \ [after nomeParaDownload filename] in below line?
response.setHeader("Content-Disposition","attachment;filename=\"" + nomeParaDownload + "\"");
</edit>
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're in trouble.

When you use JSF, JSF doesn't run an outputstream like basic J2EE does. Instead it works with a component tree that is then rendered by one of JSF's rendering engines. I'm surprised that you didn't get an Exception, in fact. Something like "Phase violation" or "Response already committed".

Code that generates non-HTML output shouldn't be JSF-based. Put it in a servlet and let your JSF code link to it. Just remember that servlets can't access the FacesContext, so any JSF objects you want to share should be accessed in a non-JSF way - for example, JSF session beans are available to servlets via the usual getSession().getAttribute() functionality.
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, thanks for the attempts to help me, Seetharaman and Tim. I'm returning here just now due to the weekend, I stayed outside the office.

One additional information is that, when I mentioned an Excel file, I wanted to say that the data was originally obtained from an Excel file that was parsed and saved into an array of bytes, in TXT format therefore.

Regarding the suggestions given to me:

@Seetharaman Venkatasamy

I've tried



but the behavior is the same, no download pop-up from the browser, the text is rendered in the browser directly.

Referring to the line



I am specifying the name of the file that users should download, so that in the pop-up window reads something like "Where do you wish to save 'marcos.txt'?"



@Tim Holloway

Thanks for the info about the inability to run non-JSF code directly from a JSF context. As you recommended, I tried to use a raw Servlet, I created a servlet class wherein I created the following method:



The aim was to create and use an OutputStream object from a non-JSF context as you suggested, so that all the output job is done and then the JSF context returns to its normal operation, after the whole method outputFile() is finished.

By doing that, I ended up having to work with 2 different response objects in my code, one from



and the other obtained from facesContext servlet



I've tried to deal with it, but I began running into other types of Exceptions, including NullPoiterException, then I quit following that path.

Tim, do you still suggest me following that track, or maybe doing it differently, still counting on raw Servlets? Actually, another question is valid, asking that to everybody:

Is there a way to produce a pop-up window in the browser from JSF code? Would there be anything like a JSF component or a specific way of dealing with the facesContext Servlet to produce such a pop-up window?

Thanks in advance!
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSF cannot call servlets. In fact, servlets cannot call servlets. You can forward from JSF/JSP/servlet to JSF/JSP/servlet, but no call-and-return. So only one Request object is involved here.

What I normally do is post a hyperlink on a JSF page to get the document and that hyperlink is the URL of the servlet. By using the "target=" attribute, the document can be made to open in a separate window or downloaded to a local file. Although in Internet Explorer there's an additional constraint. Ever since Microsoft lost the lawsuit on embedded content, IE has been forbidden from opening PDFs, Excel, Word and other documents in IE windows. So instead an actual stand-alone copy of Office or Adobe Reader has to be launched by IE.

You pretty much have to do the hyperlink route. HTTP only allows one document to be returned per request. That document can be an HTML webpage (JSF or otherwise), or it can be an Excel spreadsheet, but no one URL request can return both at the same time. ZIPfiles don't count.
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again Tim!

I'll try to follow your directions and check to see how long I can go, hopefully till the end of the tunnel

PS: I liked your sentence:

-------------------
A lot the of modern-day software development platforms are designed to permit parcelling out work to those with the best aptitude for it. A lot of modern-day business is predicated on making one person do all the work, regardless of aptitude.
-------------------

It holds true here in Brazil too, unfortunately.

Catch you later!
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Tim,

I've realized that a simple hyperlink would not work for me, given that my file is produced dynamically, it is not stored on the server prior to clicking on the hyperlink, so that I can just retrieve it. I do a need a method to perform lots of operations, generating the file, and only then making it available for download. Methods are bound to buttons, so I'll probably need one in my scenario.

I'm trying to avoid creating a button and a hyperlink to perform the operations separately. It is a lot convenient to fire both things from the same action, such as the click of a button.

Do you still have any further ideas? May anyone else suggest something also?

Thanks in advance!
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even with the file already created on the server, the code



does not retrieve the document, nothing happens at all.
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. WEB servers are NOT FILE servers. You cannot code a filename in a URL and retrieve a file from the server. You have to either have the file stored as a fixed resource in the WAR and retrieve it via an HTTP URL or you have to implement an application function to generate the document dynamically. In some cases that may end up with you creating a temporary file on the server and then having servlet logic copy the content of that temporary file to the HttpResponse stream.

Please note that you should NEVER write files in the WAR itself. If you need a temporary file, use the java.io tempfile method to create one. The webapp server will provide a safe directory location and give you a File object to access it.

So your page URL should be something like this:
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim!

I'll probably have a resolution to this by tomorrow... then I'll share my thoughts and discoveries.

Thanks again!
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No resolution as of now, a co-worker of mine told me to have a mini-project that does exactly what I need, but he has not found it yet.

I'm still trying to work with the temporary file approach, but I'm getting the following error when attempting to create the temp file:

"java.io.IOException: The system cannot find the path specified"

I attempted to create the temp file in 2 different ways:



and



I've been doing some research on it, but little have I found. Would it be related to environment variables not set, such as these things I found at Google?

TOMCAT_HOME/temp

and

TOMCAT_HOME/work

I have not managed to set those environment variable here on my Windows 7 (I know where and how, but I don't know what exactly to insert as the "Variable name" and the "Variable value").

Still working on research, if anyone has further ideas, they are much welcome
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would start out by finding out what line of code threw that exception. It's pointless to speculate if you don't even know that.
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer, Paul!

I'm not speculating, the line of code that generates the exception is



That's the one
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the moment, I've got 2 approaches to try...

I'm working on this right now: https://coderanch.com/t/213815/JSF/java/Download-files-component.

If I do not succeed, there's another snippet upon my sleeve, my co-worker gave me another code to try out some minutes ago.

Any ideas, even so, are welcome!

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marcos Silvestri wrote:Would it be related to environment variables not set, such as these things I found at Google?

TOMCAT_HOME/temp

and

TOMCAT_HOME/work



But that's still guessing. Did you consider reading the API documentation for that method? Which does indeed mention system properties (not "environment variables")?
 
Marcos Silvestri
Ranch Hand
Posts: 92
Mac Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's indeed guessing, though based on something I've read...

I'm not following that track at the moment, I'm testing another code that hopefully will work out, and then I'll share with all you.

Time to leave the office now...

Thanks again, talk later!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic