Whenever we are storing user data along with user Image. User data and Image name storing into database and image(file) storing into one local folder (E:/Upload/).
Requirements :
- Eclipse
- Java 7+
- Tomcat Server 7+
- Struts2 Jars
In Eclipse created as a Dynamic Web Project. for creating Dynamic Web Project follow below steps.
- On the main menu select File > New > Project.
- In the upcoming wizard choose Web > Dynamic Web Project.
Here we are taking basic user details and creating MySQL database table. Using Below Details for configuration
Host : localhost
MySQL port : 3306 (Default)
Database Name : Java9r
Table Name : STRUTSUSER
Database
CREATE TABLE STRUTSUSER
( NAME VARCHAR(4000),
PASSWORD VARCHAR(4000),
EMAIL VARCHAR(4000),
GENDER VARCHAR(4000),
DestPath VARCHAR(4000)
);
ImageUpload.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>Struts.xml
<center>
<h1> Upload Image To Store Into Folder And Path Save Into Database With Rename In Struts2</h1>
<form action="register" method="post" enctype="multipart/form-data">
<table>
<tr>
<td> Name: </td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Password: </td>
<td> <input type="text" name="password" /></td>
</tr>
<tr>
<td>E-Mail:</td>
<td> <input type="text" name="email" /></td>
</tr>
<tr>
<td> Gender: </td>
<td><input type="text" name="gender" /></td>
</tr>
<tr>
<td>ImageUpload: </td>
<td> <input type="file" name="myFile" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Upload"/></td>
</tr>
</table>
</form></center>
<package name="helloworld" extends="struts-default">Web.xml
<action name="register" class="java9r.com.RegisterAction">
<result name="success">ImageUpload-success.jsp</result>
<result name="error">ImageUpload-error.jsp</result>
</action>
</package>
<filter>RegisterAction.java
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
package java9r.com;RegisterDao.java
import java.io.File;
import java.io.IOException;
public class RegisterAction {
private String name,password,email,gender;
private File myFile;
private String myFileContentType;
private String myFileFileName;
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String execute() throws IOException{
int i=RegisterDao.save(this);
if(i>0){
return "success";
}
return "error";
}
}
package java9r.com;ImageUpload-success.jsp
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.commons.io.FileUtils;
public class RegisterDao {
public static int save(RegisterAction registerAction){
int status=0;
try{
File oldfile =new File("myFileFileName");
File newfile =new File( registerAction.getName() + "rename.jpg"); // changing file name
oldfile.renameTo(newfile);
File fileLocation= new File("E:/Upload/" + newfile); // file stored location
FileUtils.copyFile(registerAction.getMyFile(), fileLocation);
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/java9r","root","ravi");
PreparedStatement ps=con.prepareStatement("insert into strutsuser values(?,?,?,?,?)");
ps.setString(1,registerAction.getName());
ps.setString(2,registerAction.getPassword());
ps.setString(3,registerAction.getEmail());
ps.setString(4,registerAction.getGender());
String newFileLocation= fileLocation+ "" ;
ps.setString(5, newFileLocation);
System.out.println("inserted===================");
status=ps.executeUpdate();
}catch(Exception e){e.printStackTrace();}
return status;
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"ImageUpload-error.jsp
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>Welcome, <s:property value="name"></s:property></center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"RESULT
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>
Sorry, some error occured!
<s:include value="index.jsp"></s:include>
</body>
</html>
Hi ,how to retrive files using struts2 mysql
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteClipping Path method is followed on Photoshop utilizing very good quality instruments like the pen device, the anchor apparatus and so forth.
ReplyDeleteclipping path service provider
Any unprofessional help can in truth destroy your images. Uncouth experts might leave them turning contacted vertical. clipping path company
ReplyDeleteNotwithstanding, for cutting edge editing proficient assistance is inescapable. In the event that you wish to roll out little improvements to your photographs, you can deal with it all alone.
ReplyDeleteghost mannequin service
"Implementing FIFO (First In, First Out) is essential in industries with perishable or time-sensitive products." Minilager Oslo
ReplyDelete"AI-generated art can bring attention to underrepresented artistic styles." ai image video
ReplyDeleteConcrete paving can be adapted for decorative and artistic applications.
ReplyDeleteConcrete and Asphalt Paving
"Thrift store shopping is not only budget-friendly but also a great way to express your personal style with one-of-a-kind pieces." Thrift store gems in Cincinnati
ReplyDelete