Upload Profile Picture or File using JSP, Struts2 | Code Factory

Code Factory
3 min readNov 29, 2019

--

Donate : Link

Reference Link : Link

Applications : Link

Download Code and Jars : Link

File : temp.jsp

<%@ 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="Refresh" CONTENT="0;URL=index.action">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

File : index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>Index page</title>
</head>
<body>
<center>
<div class="boxCss">
<s:if test="#session.userProfile!=''">
<a href="javascript:void(0)" onclick="upload()" class="imgCss"> <img src=<s:property value="#session.userProfile" /> width="120" height="120"></a>
</s:if>
<s:else>
<a href="javascript:void(0)" onclick="upload()" class="imgCss"> <img src="images/admin.jpg" alt="user-img" width="120" height="120"></a>
</s:else>
<br>
<br>
<form action="uploadImg" method="post" enctype="multipart/form-data">
<div class="divCss">
<input type="file" id="image_src" name="userImage" label="Image" class="fileCss" />
<input type="submit" value="Upload" align="center" class="submitCss btn btn-primary" />
</div>
</form>
</div>
</center>
</body>
</html>

File : struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<include file="struts-default.xml" />
<package name="uploadProfilePicture" extends="struts-default">
<action name="index" class="com.codeFactory.getData">
<result name="success">index.jsp</result>
</action>
<action name="uploadImg" class="com.codeFactory.ProfilePic">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes"> image/png,image/gif,image/jpeg,image/pjpeg </param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result type="redirectAction" name="success">index</result>
<result name="input">error.jsp</result>
</action>
</package>
</struts>

File : web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>uploadProfilePicture</display-name>
<welcome-file-list>
<welcome-file>temp.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

File : getData.java

package com.codeFactory;import java.sql.DriverManager;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.opensymphony.xwork2.ActionSupport;
public class getData extends ActionSupport implements SessionAware {private Map < String, Object > session;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String query;
public String execute()
{
try {
String URL = "jdbc:mysql://localhost/test";
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection(URL, "root", "admin");
query = "select * from file_upload where id=" + 1;
ps = (PreparedStatement) con.prepareStatement(query);
rs = (ResultSet) ps.executeQuery();
while (rs.next())
{
System.out.println(rs.getInt("id"));
session.put("id", rs.getInt("id"));
System.out.println(rs.getString("file_path"));
session.put("userProfile", rs.getString("file_path"));
}
} catch (Exception e) {
System.out.println("getdata : " + e);
e.getMessage();
}
return SUCCESS;
}
@Override
public void setSession(Map < String, Object > session) {
// TODO Auto-generated method stub
this.session = session;
}
}

File : ProfilePic.java

package com.codeFactory;import java.io.File;
import java.sql.DriverManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.opensymphony.xwork2.ActionSupport;
public class ProfilePic extends ActionSupport {private File userImage;
private String userImageContentType;
private String userImageFileName;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String query;
public String execute()
{
try {
String filePath = ServletActionContext.getServletContext().getRealPath("/").concat("userImages/");
System.out.println("Image Location:" + filePath); //see the server console for actual location
File fileToCreate = new File(filePath, userImageFileName);
FileUtils.copyFile(userImage, fileToCreate); //copying source file to new file
String URL = "jdbc:mysql://localhost/test";
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection(URL, "root", "admin");
String loc = "userImages/" + userImageFileName;
query = "update file_upload set file_path='" + loc + "' where id=" + 1;
System.out.println(query);
ps = (PreparedStatement) con.prepareStatement(query);
ps.executeUpdate();
} catch (Exception e) {
System.out.println("uploadPicAction" + e);
}
return SUCCESS;
}
public File getUserImage() {
return userImage;
}
public void setUserImage(File userImage) {
this.userImage = userImage;
}
public String getUserImageContentType() {
return userImageContentType;
}
public void setUserImageContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;
}
public String getUserImageFileName() {
return userImageFileName;
}
public void setUserImageFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;
}
}

--

--