FileUpload Application in Servlet
Here We are going to see,How to create a FileUploadApplication using Java Zoom API
Directory Structure
upload1.html
<form action="uplurl" method="post" enctype="multipart/form-data"> Select File1 <input type="file" name="file1" /> Select File2 <input type="file" name="file2" /> <input type="Submit" value="Upload" /> </form>
web.xml
<web-app> <servlet> <servlet-name>uplsrv</servlet-name> <servlet-class>UplSrv1</servlet-class> </servlet> <servlet-mapping> <servlet-name>uplsrv</servlet-name> <url-pattern>/uplurl</url-pattern> </servlet-mapping> </web-app>
UplSrv1.java
//UplSrv1.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javazoom.upload.*; import java.util.*; public class UplSrv1 extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter pw = res.getWriter(); try { // set location for saving uploded 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 uploded files pw.println("The uploded files are "); Hashtable ht = nreq.getFiles();// gives all uploded 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
Jars required in classpath
servlet-api.jar
uploadbean.jar
uploadbean.jar
Jars required in lib Folder
(See the directory structure)
(See the directory structure)
Compile the ServletProgram
c:\JavaZoomUpload\WEB-INF\classes>javac *.java
Running the Application
Copy the JavaZoomUpload Application and paste it to web-apps folder of Tomcat
Executing the Application
Start tomcat server.and then give the following request from browser window
http://localhost:2012/JavaZoomUpload/upload1.html
You will get the screen
Select any two file,and click upload button.The file will be uploaded and stored in C:\store folder of the server.(in our case,It will be stored in our c:\store folder.
Upon successful upload,you will get the success screen
Hope,You like this tutorial.
Thank You