How to upload Image in database using Struts 2

In this tutorial, we are going to explain how to Upload/Insert images in the database using Struts 2. First of all, we will add the enctype="multipart/form-data" attribute in the form tag. The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

Note:The enctype attribute can be used only if method=”post”.

Software Used

  1. Eclipse IDE
  2. MySQL Database
  3. JDK 8
  4. Tomcat 8

Required JARS

To upload the image in database these JARS are required.

  1. All struts 2 core jars
  2. commons-fileupload-x.x.x.jar

Project Structure in Eclipse

How to upload Image in database using Struts 2

Step 1: The very first step is to create a table in the database to store the data and datatype of image column must be BLOB.

 CREATE TABLE `image_upload` (
  `image` BLOB,
  PRIMARY KEY (`id`)
) ;

Step 2: Add the struts 2 filters in web.xml.

Step 3: Create the Action Class to handle the data. In my case, we have created the database Connection in same class.
package org.websparrow;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import com.opensymphony.xwork2.ActionSupport;

public class ImageAction extends ActionSupport {
	private String name;
	private File image;
	private String msg;

	// create database connection
	public static Connection connect() throws Exception {
		return (Connection) DriverManager.getConnection("jdbc:mysql://", "root", "");

	public String execute() {
		try {
			PreparedStatement preparedStatement = connect().prepareStatement("INSERT INTO IMAGE_UPLOAD(NAME,IMAGE) VALUES(?,?)");
			preparedStatement.setString(1, name);
			// for inserting image in database
			FileInputStream inputStream = new FileInputStream(image);
			preparedStatement.setBinaryStream(2, inputStream);
			int i = preparedStatement.executeUpdate();
			if (i > 0) {
				setMsg("Image successfully inserted.");
			} else {
				setMsg("Something gone wrong.");
		} catch (Exception e) {
		return "SUCCESS";

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public File getImage() {
		return image;

	public void setImage(File image) {
		this.image = image;

	public String getMsg() {
		return msg;

	public void setMsg(String msg) {
		this.msg = msg;

Step 4: Map the action in struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	<constant name="struts.devMode" value="true" />
	<package name="default" extends="struts-default" namespace="/">
		<action name="imageupload" class="org.websparrow.ImageAction">
			<result name="SUCCESS">/index.jsp</result>

Step 5: User interface page

<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<title>Struts 2 image insert example-</title>
<body style="background-color: #f8f8f8; margin-left: 200px; margin-top: 40px; font-size: 25px;">

	<h1>Struts 2 image insert example</h1>
	<s:form action="imageupload" method="POST" enctype="multipart/form-data">
		<s:textfield name="name" label="Name" maxlength="20" />
		<s:file name="image" label="Select Image" />
		<s:submit name="sumbit" value="Submit" />
	<div style="margin-top: 40px; font-size: 30px; color: green;">
		<s:property value="msg" />


Now you are all done. Run your application and check the table in the database.

Screen 1

How to upload Image in database using Struts 2

Screen 2

How to upload Image in database using Struts 2

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.