- File Download Movie
- Jsf Primefaces File Download Example
- Jsf File Download
- Jsf Documentation
- Jsf Download File From Bean
- File Download Share
- Jsf 2.3 Download
In this example, we are going to learn about uploading and downloading of a file through JSP.
File Input output are very important operations. Here we are going to read and write a file using JSP.
JSP File Upload
The computer does not have enough hardware resources to cope with the opening of the JSF file. Drivers of equipment used by the computer to open a JSF file are out of date. If you are sure that all of these reasons do not exist in your case (or have already been eliminated), the JSF file should operate with your programs without any problem. Next PDF file bytes are being read the same way we have shown you in the Java Download file tutorial some time ago. PDF bytes are being read in the while loop and written to the response OutputStream. After the streams are closed there is one important thing to do. You need to signal the JSF that the HTTP response for this request has been.
- We can upload any files using JSP.
- It can be a text file, binary file, image file or any other document.
- Here in case of file uploading, only POST method will be used and not the GET method.
- Enctype attribute should be set to multipart/form-data.
Example: Using Action
In this example, we are uploading a file using IO object
Action_file.jsp
Action_file_upload.jsp
Explanation of the code:
Action_file.jsp
Code Line 12-18: Here we are creating form with file field, which will upload file to the server and action will be passed to action_file_upload.jsp
Action_file_upload.jsp
Code Line 20: Here we are giving the file path to a particular path
Code Line 23-38: Here we check whether the content type is multipart/form-data. If that is the case, then the content is of file type, and it is read. After file being read, it is written into the temporary file and then the temporary file gets converted to the main file.
When you execute the above code, you get the following output
Output:
We are uploading file using choose file button option and upload file button will upload the file to the server to the path which is provided.
Example: Using JSP operations
In this example, we are going to upload a file using JSP operations.
We will take a form which will have 'upload' button and when you click on upload button then the file will be uploaded.
Uploading_1.jsp
Explanation of the code:
Code Line 11-12: Here we are taking a form which has action on servlet guru_upload which will pass through a method POST. Also, here we enctype i.e. attribute which specifies how form data should be encoded and sent to server and it is only used with POST method. Here we are setting as multipart/form-data which is for the file(as data will be large).
Code Line 13: Here we are specifying guru_file element with type file and giving size as 50.
Code Line 15: This is a submit type button with name 'Upload' on it through which action servlet will be called and request will processed into that and file will be read and write into the servlet.
Guru_upload.java
Explanation of the code:
Code Line 12-14: Here we will have to import org.apache.commons library into the configuration of the code. We will have to import fileupload class from org.apache.commons library.
Code Line 23: Here we have doPost() method, which will be called as we are passing POST method in JSP and it will request and response objects as its parameters
Code Line 26: Here we are creating an object of ServletFileUpload class from fileUpload package from org.apache.commons library which will check whether there are any file objects in JSP. If any found then those file object will be taken from request.
Code Line 27-32: We will iterate the number of files by checking how many file item are present in multiparts object which is a list object (if we upload more than one file) and save it into c:/guru/upload folder with the filename which has been provided. We are writing the file using write method of the fileobject into the folder which has been mentioned.
Code Line 34: If there is no exception then we are setting attribute in request as gurumessage with value 'File uploaded successfully'.
Code Line 35-36: If exception occurs then setting message that 'File upload failed'
Code Line 40: If file not found then setting message as 'No file found'
Code Line 42: Forwarding request using requestdispatcher object to result.jsp with request and response objects.
Result.jsp
Explanation of the code:
Code Line 10: Here we are getting the attribute from the request object with value gurumessage into a string object.
Code Line11: Here we are printing that message.
When we execute the above code we get the following output
Output:
We get a form wherein there is fields to choose a file from directory. Once the file is selected then we have to click on the upload button.
Once the upload button is click we get the message that file is uploaded successfully.
In the below diagram we can see that file had been uploaded in c:/guru/upload folder.
Downloading File:
In this example, we are going to download a file from a directory by clicking on the button.
Downloading_1.jsp
Explanation of the code:
File Download Movie
Code Line 10: Here we have given link to download a file from folder c:/guru/upload using servlet guru_download .
Guru_download.java
Explanation of the code:
Code Line 3-5: Here we are importing FileInputStream , IO Exception and PrintWriter from the java.io package.
Jsf Primefaces File Download Example
Code Line 15: We are defining guru_download servlet which extends HttpServlet.
Code Line 18: As we have defined a href, which will be enclosed in URL so GET method will get processed (doGet will be called in servlet) which also encloses request and response objects.
Code Line 19-20: We are setting content Type in response object and also get writer object from response.
Code Line 21-22: Defining a variable as gurufile as value test.txt and gurupath as c:/guru/upload/
Code Line 23-25: We are setting the content type using response object and we use setHeader method which sets header into the response object as the filename which has been uploaded.
Code Line 27-28: We are creating FileInputStream in which we will add gurupath+gurufile.
Code Line 31-33: Here we have taken a while loop which will run till the file is read,hence we have given condition as != -1. In this condition we are writing using printwriter object out.
When you execute the above code you will get the following output
Output:
Output:
We have to click on downloading_1.jsp we will get a hyperlink as 'Download Here'. When you click on this hyperlink file, it will downloaded into the system.
Summary:
- We have learnt about registration form to register into any application
- Learnt more about how the login in and logout forms works out.
- Also learnt about uploading and downloading of a file through JSP.
Active1 year ago
Is there any way of providing a file download from a JSF backing bean action method?I have tried a lot of things. Main problem is that I cannot figure how to get the
OutputStream
of the response in order to write the file content to. I know how to do it with a Servlet
, but this cannot be invoked from a JSF form and requires a new request.How can I get the
BalusCOutputStream
of the response from the current FacesContext
?890k312312 gold badges32663266 silver badges32963296 bronze badges
Jsf File Download
zmedazmeda1,42077 gold badges2929 silver badges5454 bronze badges
4 Answers
Introduction
You can get everything through
ExternalContext
. In JSF 1.x, you can get the raw HttpServletResponse
object by ExternalContext#getResponse()
. In JSF 2.x, you can use the bunch of new delegate methods like ExternalContext#getResponseOutputStream()
without the need to grab the HttpServletResponse
from under the JSF hoods. On the response, you should set the
Content-Type
header so that the client knows which application to associate with the provided file. And, you should set the Content-Length
header so that the client can calculate the download progress, otherwise it will be unknown. And, you should set the Content-Disposition
header to attachment
if you want a Save As dialog, otherwise the client will attempt to display it inline. Finally just write the file content to the response output stream.Most important part is to call
FacesContext#responseComplete()
to inform JSF that it should not perform navigation and rendering after you've written the file to the response, otherwise the end of the response will be polluted with the HTML content of the page, or in older JSF versions, you will get an IllegalStateException
with a message like getoutputstream() has already been called for this response
when the JSF implementation calls getWriter()
to render HTML.Turn off ajax / don't use remote command!
You only need to make sure that the action method is not called by an ajax request, but that it is called by a normal request as you fire with
<h:commandLink>
and <h:commandButton>
. Ajax requests and remote commands are handled by JavaScript which in turn has, due to security reasons, no facilities to force a Save As dialogue with the content of the ajax response.In case you're using e.g. PrimeFaces
<p:commandXxx>
, then you need to make sure that you explicitly turn off ajax via ajax='false'
attribute. In case you're using ICEfaces, then you need to nest a <f:ajax disabled='true' />
in the command component.Generic JSF 2.x example
Generic JSF 1.x example
Common static file example
In case you need to stream a static file from the local disk file system, substitute the code as below:
Common dynamic file example
In case you need to stream a dynamically generated file, such as PDF or XLS, then simply provide
output
there where the API being used expects an OutputStream
.Jsf Documentation
E.g. iText PDF:
E.g. Apache POI HSSF:
Note that you cannot set the content length here. So you need to remove the line to set response content length. This is technically no problem, the only disadvantage is that the enduser will be presented an unknown download progress. In case this is important, then you really need to write to a local (temporary) file first and then provide it as shown in previous chapter.
Utility method
If you're using JSF utility library OmniFaces, then you can use one of the three convenient
Faces#sendFile()
methods taking either a File
, or an InputStream
, or a byte[]
, and specifying whether the file should be downloaded as an attachment (true
) or inline (false
).Yes, this code is complete as-is. You don't need to invoke
Jasper de VriesresponseComplete()
and so on yourself. This method also properly deals with IE-specific headers and UTF-8 filenames. You can find source code here.9,68055 gold badges3838 silver badges7575 bronze badges
BalusCBalusC890k312312 gold badges32663266 silver badges32963296 bronze badges
John MendesJohn MendesJsf Download File From Bean
34922 gold badges55 silver badges1515 bronze badges
Koray TugayKoray Tugay9,6783030 gold badges126126 silver badges240240 bronze badges
File Download Share
here is the complete code snippet http://bharatonjava.wordpress.com/2013/02/01/downloading-file-in-jsf-2/
You may change the file reading logic in case you want file to get generated at runtime.
Bharat SharmaBharat Sharma