The process of selecting file from the client machine file system and sending that file to the server machine system is known as file uploading. File uploading and downloading using servlet-api is a complex process, that is why industry uses JavaZoom 3rd party API.
download it from www.javazoom.net as zip file.
JavaZoom-API comes in the form of three jar file.
1. uploadbean.jar 2. cos.jar 3. struts.jar
IMPORTANT CLASSES OF JAVAZOOM.API
1. MultiPartFormDataRequest: Represents all the uploaded files from servlet/jsp program. To create this object we need HttpServletRequest object.
2. UploadBean: Allows to specify the file uploading related roperties and to complete file uploading operation.
3. UploadFile: Represents each file that has been uploaded. To select file from the client machine file system we can use the file uploading component.
<input type='file' name='fs'>
While performing file uploading the form page must generate “POST” methodology based request having the encryption type=“multipart/form-data”
Step 1: Prepare the deployment directory structure of web application.
Step 2: Develop the source code of above servlet program or web Application
UplSrv.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javazoom.upload.*; import java.util.*; public class UplSrv extends HttpServlet { public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter pw = res.getWriter (); try { //set location for saving uploaded files UploadBean upb = new UploadBean (); upb.setFolderstore ("c:/store"); upb.setOverwrite (false); MultipartFormDataRequest nreq = new MultipartFormDataRequest(req); upb.store (nreq); //completes files uploding // display names of the uploaded files pw.println ("The uploaded files are"); Hashtable ht = nreq.getFiles (); //gives all uploaded files Enumeration e = ht.elements (); while (e.hasMoreElements ()) { UploadFile file = (UploadFile) e.nextElement (); pw.println (file.getFileName () + " "); } //while } //try catch (Exception e) { pw.println (e); } //catch } //dopost () } //class
Explanation of the above program
In the above program our intention is uploading two files.
In the above program all the five packages are imported. Here javax.servlet
package is imported because the method doPost (-,-) throws exceptions such as ServletException and IOException for this reason we import javax.servlet package. As the class extends from HttpServlet, this HttpServlet class present in the javax.servlet.http package and HttpServletRequest and HttpServletResponse interfaces are also present within the javax.servlet.http package. That’s why it’s necessary to import the javax.servlet.http and also import javazoom.upload.*;
UploadBean, MultipartFormDataRequest classes are present in javazoom. uploadpackge, so it is necessary to import javazoom.upload package. Also, import java.util.* package because Hashtable class, Enumeration interface are present in java.utilPackage, and also import:
• Print Writer pw= res.getWriter ( );
Within the service method i.e. service (-,-), to print the output in the browser window PrintWriter class is used. PrintWriter class and the service (-,-) exception i.e IOException are present in java.io package. So, java.io.*; package is imported.
• res.setContentType (“text/html”);
set the content Type
• UploadBean upb = new UploadBean ();
Creates the object of Upload Bean class
• upb.setFolderstore (“c:/store”);
Sets location for saving uploaded files
• MultipartFormDataRequest nreq = new MultipartFormDataRequest (req);
Creates the object MultipartFormDataRequest
• upb.store (nreq);
This method completes the file uploading.
• Hashtable ht = nreq.getFiles ();
Gives all uploaded files in Hashtable
• Enumeration e = ht.elements ();
Collects all files from Hashtable
• UploadFile file = (UploadFile) e.nextElement ();
Reads files sequentially from Enumeration
• pw.println (file.getFileName ()+ “ ”);
Gets the uploaded file name
upload.html
<center> <form method=“post” action=“upload” name=“upload” enctype=“multipart/form-data”> File 1 : <input type= “file” name=“uploadfilel” value= ‘Browse’/> <br> <br> File 2 : <input type=“file” name= “uploadfile2” value= ‘Browse’/> <br> <br> <input type=“Submit” value= “Upload”> </form> </center>
Step 3: Compile the source files of all servlet programs.
Step 4: Configure all the four servlet programs in web.xml file having four different url patterns.
web.xml
<web-app> <servlet> <servlet-name> abc </servlet-name> <servlet-class> UplSrv </servlet-class> </servlet> <servlet-mapping> <servlet-name> abc </servlet-name> <url-pattern> /upload </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> upload.html </welcome-file> </welcome-file-list> </web-app>
Step 5: Start the server (Tomcat).
Step 6: Deploy the web application and Copy UploadApp folder to Tomcat_home\webapps folder.
Step 7: Test the web application.
Open browser window type this urls: https://ecomputernotes.com:2020/UploadApp/ upload.html