File Input output are very important operations. Here we are going to read and write a file using JSP.

JSP File Upload

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: 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. 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.