JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Struts 2 File Uploading

  • In struts2 file uploading can done wih the help of built-in Intercepter FileUploadInterceptor
  • We use Struts 2 tags to build our form..
  • The encoding type of the form should be set to multipart/form-data and the HTTP method should be set to post.

Example:-The Folowing Example takes a file from user and Display its name and type in another page.

index.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">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type"
  content="text/html; charset=ISO-8859-
1">

</head>
<body>
  <s:form action="fileUpload" method="post"
enctype="multipart/form-data">
    <s:file name="file" label="Browse" />
    <s:submit />
  </s:form>

</body>
</html>

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>
 <package name="fileUploadPackage" extends="struts-default">
 <action name="fileUpload" class="myaction.FileUploadAction">
 <interceptor-ref name="fileUploadStack" />
 <result name="input">/index.jsp</result>
 <result name="success">/success.jsp</result>
 </action>
 </package>
</struts>

  • We need to have a File attribute and the corresponding getter and setter methods
    in the action to receive the  file.

FileUploadAction.java
package myaction;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
  private File file;
  private String fileContentType;
  private String fileFileName;
    
  public File getFile() {
    return file;
  }
  public void setFile(File file) {
    this.file = file;
  }
  public String getFileContentType() {
    return fileContentType;
  }
  public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
  }
  public String getFileFileName() {
    return fileFileName;
  }
  public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
  }
  @Override
  public String execute() throws Exception {
  
    return SUCCESS;
  }
    
   
}

When the file is uploaded successful, the user will be forwarded to the success page,
where the details regarding the file upload will be displayed.

success.jsp
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"
>
</head>
<body>
<h3>You have uploaded the following file.</h3>

 File Name : <s:property value="fileFileName"/> <br>
 Content Type :<s:property value="fileContentType"/>
</body>
</html>


Output:



When the user selects a file and upload it. The following success page will be
displayed to the user.



Download Source without lib
Download War with lib