Searching...
Sunday, June 1, 2014

Upload Image To Store Into Folder And Path Save Into Database With Rename In Struts2

June 01, 2014

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 :

  1. Eclipse 
  2. Java 7+ 
  3. Tomcat Server  7+ 
  4. Struts2 Jars  


In Eclipse created as a Dynamic Web Project. for creating Dynamic Web Project follow below steps. 
  1. On the main menu select File > New > Project. 
  2. 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" %>  
    <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>
 Struts.xml
   <package name="helloworld" extends="struts-default">
 <action name="register" class="java9r.com.RegisterAction"> 
<result name="success">ImageUpload-success.jsp</result> 
<result name="error">ImageUpload-error.jsp</result> 
</action>
</package>
 Web.xml
<filter>
        <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>
RegisterAction.java
package java9r.com;

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"; 

}  

RegisterDao.java
package java9r.com;

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; 
}

}

ImageUpload-success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    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>
ImageUpload-error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    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> 
  RESULT
Newer Post
Previous
This is the last post.

10 comments:

  1. Hi ,how to retrive files using struts2 mysql

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Clipping Path method is followed on Photoshop utilizing very good quality instruments like the pen device, the anchor apparatus and so forth.
    clipping path service provider

    ReplyDelete
  5. Any unprofessional help can in truth destroy your images. Uncouth experts might leave them turning contacted vertical. clipping path company

    ReplyDelete
  6. Notwithstanding, 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.
    ghost mannequin service

    ReplyDelete
  7. "Implementing FIFO (First In, First Out) is essential in industries with perishable or time-sensitive products." Minilager Oslo

    ReplyDelete
  8. "AI-generated art can bring attention to underrepresented artistic styles." ai image video

    ReplyDelete
  9. Concrete paving can be adapted for decorative and artistic applications.
    Concrete and Asphalt Paving

    ReplyDelete
  10. "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

ads2