File Upload in Web Server using JSP and Servlet - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

File Upload in Web Server using JSP and Servlet

Share This
Code for index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <div> <h3>Choose File to Upload in Server</h3> <form action="UploadServ" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="upload" /> </form> </div> </body> </html>
Code for result.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>File Upload Example in JSP and Servlet - Java web application</title> </head> <body> <div id="result"> <h3>${requestScope["message"]}</h3> </div> </body> </html>
Source code for UploadServ.java

package com.t4b.demo.fileupload; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.sql.Timestamp; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class UploadServ */ @WebServlet("/UploadServ") public class UploadServ extends HttpServlet { private final String UPLOAD_DIRECTORY = "D:\\Development\\uploads"; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String rootpath = getServletContext().getRealPath("WEB-INF/../"); File tempfile = new File(rootpath); String fullPathToYourWebappRoot = tempfile.getCanonicalPath(); String filepath = ""; String relativepath = ""; // process only if its multipart content if (ServletFileUpload.isMultipartContent(request)) { try { List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : multiparts) { if (!item.isFormField()) { String name = new File(item.getName()).getName(); relativepath = "uploads" + File.separator + name; filepath = fullPathToYourWebappRoot + File.separator + relativepath; item.write(new File(filepath)); } } // File uploaded successfully out.print("File Uploaded Successfully! Get it from here"); } catch (Exception ex) { out.print("File Upload Failed due to " + ex); } } else { out.print("Sorry this Servlet only handles file upload request"); } } }



Happy Exploring!

No comments:

Post a Comment