Tuesday 26 February 2013

File Upload



To Upload the File we use <s:file>  struts tag.


Web.xml
  Following is the content of web.xml file:
<?xml version="1.0" encoding="UTF-8" ?>

   Demo
   
    Image.jsp
   
 
   
            struts2
            
                  org.apache.struts2.dispatcher.FilterDispatcher
            
   
      
            struts2
            /*
      

Struts.xml
  struts.xml configuration file as follows:
<?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">


      
      
      
           
   
     
      
       
          2097152
          
            image/png,image/gif,image/jpeg,image/pjpeg
          
       
       
       successFile.jsp
       Image.jsp
        
   

Image.jsp
We have to create  Image.jsp in the WebContent folder, to upload image.
Code-
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
Upload
</head>

<body>

File Upload

</body> </html>
successFile.jsp
When File Upload Successfully then successFile.jsp is called.
Code-
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
Upload Image
</head>
<body>
    

File Upload Example

Content Type: <s:property value="userImageContentType"/> File Name: <s:property value="userImageFileName"/> Uploaded Image: <img src="<s:property value="userImageFileName"/>"/> </body> </html>
FileUploadAction.java
 The next step is to create an Action method that takes care of sending the email. Let us create a new class called FileUploadAction.java.
Code-
package view;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements
       ServletRequestAware {
   private File userImage;
   private String userImageContentType;
   private String userImageFileName;
   private HttpServletRequest servletRequest;

   public String execute() {
       try {
           String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
           System.out.println("Server path:" + filePath);
           File fileToCreate = new File(filePath, this.userImageFileName);
           FileUtils.copyFile(this.userImage, fileToCreate);
       } catch (Exception e) {
           e.printStackTrace();
           addActionError(e.getMessage());
           return INPUT;
       }
       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;
   }
   @Override
   public void setServletRequest(HttpServletRequest servletRequest) {
       this.servletRequest = servletRequest;
   }
}
Output:-

When File successfully Uploaded.

3 comments :

  1. How to attach file to email...?

    ReplyDelete
  2. To attach file form email you required mail.jar and MimeMultipart interface.
    I will show you how to send email with attachment with example.
    for that check my new post.
    and visit again for any quires.

    I am happy to help u.

    ReplyDelete
  3. i want send multiple email from one jsp form eg- gmail,yahoo,reddifmail etc. plz send me solution

    ReplyDelete

    Categories

    Text Widget

    Followers