Uploading files in Struts
Struts has default interceptor (in-build interceptor) for uploading files, however not that these files will get stored in temporary directory they should be processed and stored in permanent location.  (Refer: Default Interceptors in Struts)

Lets see an example of uploading an image using Struts,

Directory Structure
Structure

We have created 2 jsp files, one dao.java for this example.

upload.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%> 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Uploading Image</title>
    </head>
    <body>
        <s:form action="userImage" method="post" enctype="multipart/form-data"> 
            <s:file name="userImage" label="Image" />  
            <s:submit value="Upload" align="center" />  
        </s:form>  
    </body>
</html>

success.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Image show</title>
    </head>
    <body>
          Uploaded Successfully!                                                 
    </body>
</html>

dao.java

package Dao;

import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;


public class dao extends ActionSupport implements ServletRequestAware  {
    File imageupload;
    HttpServletRequest servletRequest; 
    String filename;

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public HttpServletRequest getServletRequest() {
        return servletRequest;
    }

    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }

    public File getImageupload() {
        return imageupload;
    }

    public void setImageupload(File imageupload) {
        this.imageupload = imageupload;
    }
    
    public String execute()
    {
        try
        {
        String path=servletRequest.getSession().getServletContext().getRealPath("/").concat("images"); 
        System.out.println(path);
        File create = new File(path, this.filename);  
        FileUtils.copyFile(this.imageupload, create);
        }
        catch(Exception e)
        {
            
        }      
        return "success";  
    }   
}

struts.xml

<struts>
    <package name="default" extends="struts-default">
        <action name="userImage" class="Dao.dao">
             <interceptor-ref name="fileUpload">  
                <param name="maximumSize">2000000</param>  
                <param name="allowedTypes">  
                    image/png,image/gif,image/jpeg,image/pjpeg  
                                </param>  
            </interceptor-ref>  
            <interceptor-ref name="defaultStack"></interceptor-ref>  
            <result name="success">success.jsp</result> 
            <result name="input">upload.jsp</result>
        </action>
    </package>
</struts>

Output:
op1
op2
Uploaded Successfully!

Now check server.log to know the location where the image got saved,

Image Location: E:\NetBeans\Struts2FUL\build\web\images

 

op4

 

 

 

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *