How To Upload Image in Folder and Path in Database Using Servlet, JSP %
How To Upload Image in Folder and Path in Database Using Servlet, JSP %
JavaTechCodes
Java Programming Examples on Servlet, JSP, Spring, Hibernate, JavaScript & much more
Today we will discuss How to upload/store an image in folder and image path in the Database table using Servlet
and JSP. we will create a JSP form to upload the image.After Submitting the form the image le will be stored in
speci ed folder and path in the Database table.To store the image in the folder we create a le directory in which
image will be stored and then create a database connection in which le path will be saved.
1-index.jsp
2-FileUpload.java
JSP le- index.jsp
1 <head>
2 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
3 <title>How to upload image in folder & path in database using Serlet,JSP</title>
4 </head>
5 <body>
6 <form action="FileUpload" method="post" enctype="multipart/form-data">
7 <center>
8 <h1>How to upload image in folder & path in database using Serlet,JSP</h1>
9 <table>
10 <tr><td>Roll No.:</td><td><input type="text" name="s_rollno" /></td></tr>
11 <tr><td>First Name:</td><td><input type="text" name="s_first" /></td></tr>
12 <tr><td>Last Name:</td><td><input type="text" name="s_last" /></td></tr>
13 <tr><td>Gender:</td><td><input type="radio" name="gender" value="male" checked> Male<input t
14 <tr><td>Email:</td><td><input type = "text" name="s_email" ></td></tr>
15 <tr><td>Mobile No:</td><td><input type="text" name="s_mobile"></td></tr>
16 <tr><td>File Upload:</td><td><input type="file" name="image"></td></tr>
17 <tr><td></td><td><input type="submit" value="Submit"/></td></tr>
18 </table>
19 </center>
20 </form>
21 </body>
Servlet File-FileUpload.java
1 package com.javatech;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.annotation.MultipartConfig;
9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.Part;
14
15 import com.mysql.jdbc.Connection;
16
17 import java.sql.*;
18 import java.util.ArrayList;
19
20 @WebServlet("/FileUpload")
21 @MultipartConfig(fileSizeThreshold=1024*1024*2,
22 maxFileSize=1024*1024*5)
23 public class FileUpload extends HttpServlet {
24 private static final String SAVE_DIR="images";
25
26 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws S
27
28 response.setContentType("text/html");
http://www.javatechcodes.com/servlet/upload-image-in-folder-using-servlet-jsp/ 2/4
27/12/2017 How to upload image in folder and path in Database using Servlet,JSP %
http://www.javatechcodes.com/servlet/upload-image-in-folder-using-servlet-jsp/ 3/4
27/12/2017 How to upload image in folder and path in Database using Servlet,JSP %
Simple CRUD operation How to Create Registration How to Search Data from
using Servlet,JSP Form in JSP Database in Servlet
How to retrieve Data From How to insert, update and How to Download the File
Database and Display in ... delete data in Servlet,J... using Servlet
Category: JSP Servlet Tags: connection , databse , le path , le upload , folder , jsp , mysql , servlet
http://www.javatechcodes.com/servlet/upload-image-in-folder-using-servlet-jsp/ 4/4