50% found this document useful (2 votes)
1K views

How To Upload Image in Folder and Path in Database Using Servlet, JSP %

This document discusses how to upload an image file to a folder and store the file path in a database table using Servlets and JSP. It involves creating a JSP form to select an image file. When submitted, the file will be saved to a specific folder on the server and the file path stored in a database table. Code examples show how to create the database table, JSP form, and Servlet class to handle the file upload and data storage. Upon successful upload, a confirmation message is displayed.

Uploaded by

Yanno Dwi Ananda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
1K views

How To Upload Image in Folder and Path in Database Using Servlet, JSP %

This document discusses how to upload an image file to a folder and store the file path in a database table using Servlets and JSP. It involves creating a JSP form to select an image file. When submitted, the file will be saved to a specific folder on the server and the file path stored in a database table. Code examples show how to create the database table, JSP form, and Servlet class to handle the file upload and data storage. Upon successful upload, a confirmation message is displayed.

Uploaded by

Yanno Dwi Ananda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

27/12/2017 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

How to upload image in folder and path in Database


using Servlet,JSP
By Anshul Gupta | June 19, 2017 0 Comment

How to upload image in folder and path in Database using Servlet/JSP

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.

File Required for upload image-

1-index.jsp
2-FileUpload.java

Create Database Table-

1 CREATE TABLE file_upload(


2    rollno NUMERIC(10) NOT NULL,
3    first_name varchar(40) NOT NULL,
http://www.javatechcodes.com/servlet/upload-image-in-folder-using-servlet-jsp/ 1/4
27/12/2017 How to upload image in folder and path in Database using Servlet,JSP %

4    last_name varchar(40) NOT NULL,


5    gender varchar(40) NOT NULL,
6    email_id varchar(40) NOT NULL,
7    mobileno varchar(40) NOT NULL,
8    filepath varchar(100) NOT NULL,
9   CONSTRAINT rollno_pk PRIMARY KEY (rollno)
10 );

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 %

29 PrintWriter out= response.getWriter();


30 String savePath = "D:" + File.separator + SAVE_DIR;
31          File fileSaveDir=new File(savePath);
32          if(!fileSaveDir.exists()){
33              fileSaveDir.mkdir();
34          }
35     int rollno= Integer.parseInt(request.getParameter("s_rollno"));
36 String firstname = request.getParameter("s_first");
37     String lastname = request.getParameter("s_last");
38     String gender = request.getParameter("gender");
39     String emailid = request.getParameter("s_email");
40     String mobile = request.getParameter("s_mobile");
41     Part file = request.getPart("image");
42     
43     String fileName=extractfilename(file);
44     file.write(savePath + File.separator + fileName);
45     String filePath= savePath + File.separator + fileName ;
46     
47     
48     try {
49 Class.forName("com.mysql.jdbc.Driver");
50 Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost
51     PreparedStatement ps= con.prepareStatement("insert into file_upload(rollno,first
52     ps.setInt(1,rollno);
53     ps.setString(2,firstname);
54     ps.setString(3,lastname);
55     ps.setString(4,gender);
56     ps.setString(5,emailid);
57     ps.setString(6,mobile);
58     ps.setString(7,filePath);
59     
60     int i=ps.executeUpdate();
61     if(i>0)
62     {
63      out.print("<h1>file uploaded successfully</h1>");
64     }
65     con.close();
66 } catch (ClassNotFoundException | SQLException e) {
67 e.printStackTrace();
68 }  
69 }
70  
71 private String extractfilename(Part file) {
72         String cd = file.getHeader("content-disposition");
73         String[] items = cd.split(";");
74         for (String string : items) {
75             if (string.trim().startsWith("filename")) {
76                 return string.substring(string.indexOf("=") + 2, string.length()-1);
77             }
78         }
79         return "";
80     }
81
82 }

Output- le successfully uploaded in the folder and le path in the database.

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 %

You might also like

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

You might also like