This project is continuation of my previous video JAVA JSP file upload example
In this video I have shown how you can host a project in tomcat server. Then you can upload a image and show the image on the JSP page.
1. Project Structure
The project needs 2 jar files. The jar files should be added in to lib inside the web-inf.
2. index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <center> <h1>Upload File Form</h1> <form action="FileUploadHandler" enctype="multipart/form-data" method="post"> Enter File Name <input type="text" name="file_name"><br> Select<input type="file" name="file2" /><br> <input type="submit" value="upload" /> </form> <% String file_name=(String)request.getParameter("filename"); if(file_name!=null){ out.println(file_name+" File uploaded successfuly"); %><br> <img src="http://localhost:8080/uploaded_files/<%=file_name%>"> <% } %> </center> </body> </html> |
The index.jsp page has the form to submit the image to the FileUploadHandler servlet.
3.FileUploadHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package com.chilyfacts.com; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; 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.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUploadHandler extends HttpServlet { private static final long serialVersionUID = 1 ; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { String file_name = null; String file_name2=""; response.setContentType("text/html"); PrintWriter out = response.getWriter(); boolean isMultipartContent = ServletFileUpload.isMultipartContent(request); if (!isMultipartContent) { return; } FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List < FileItem > fields = upload.parseRequest(request); Iterator < FileItem > it = fields.iterator(); if (!it.hasNext()) { return; } while (it.hasNext()) { FileItem fileItem = it.next(); boolean isFormField = fileItem.isFormField(); if (isFormField) { if (file_name == null) { if (fileItem.getFieldName().equals("file_name")) { file_name = fileItem.getString(); } } } else { if (fileItem.getSize() > 0) { // fileItem.write(new File("E:\\uploaded_files\\" + fileItem.getName())); file_name2=fileItem.getName(); fileItem.write(new File("C:\\Program Files (x86)\\Apache Software Foundation\\Tomcat 8.5\\webapps\\uploaded_files\\" + file_name2)); } } } } catch (Exception e) { e.printStackTrace(); } finally { out.println("<script type='text/javascript'>"); out.println("window.location.href='index.jsp?filename="+file_name2+"'"); out.println("</script>"); out.close(); } } } |
FileUploadHandler.java servlet will save the file in the path specified. Please note that the path I specified here is the path of installation file of my tomcat server.
4.Now the project is exported as WAR file from the eclipse IDE
You can export the project by right clicking on the project and select Export > WAR file.
5.Now login in Tomcat Web Application Manager, The screen shot shown below.
6.Now Deploy the WAR file of the project by selecting .
7.Once the project is Deployed, You can open the project from the Applications section and try uploading the image
8.Download the complete project here
JSP_File_Upload