Wednesday 8 February 2012

Struts2 Upload Multiple Files


Directory Structure

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2.3_FileUpload</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>


struts.properties
struts.custom.i18n.resources=messages

messages.properties
 title=Struts2  multiple Files Upload Application
description=File Description
attachment1=Attachement File-1
attachment1=Attachement File-2
attachment1=Attachement File-3
button=Submit

UploadMultipleFiles.java
package com.usr.fileupload.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class UploadMultipleFiles {
private List<File> attachment=new ArrayList<File>();
private List<String> attachmentContentType=new ArrayList<String>();
private List<String> attachmentFileName=new ArrayList<String>();
public List<File> getAttachment() {
    return attachment;
}
public void setAttachment(List<File> attachment) {
    this.attachment = attachment;
}
public List<String> getAttachmentContentType() {
    return attachmentContentType;
}
public void setAttachmentContentType(List<String> attachmentContentType) {
    this.attachmentContentType = attachmentContentType;
}
public List<String> getAttachmentFileName() {
    return attachmentFileName;
}
public void setAttachmentFileName(List<String> attachmentFileName) {
    this.attachmentFileName = attachmentFileName;
}
public String upload(){
    return "success";
}
}

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>
    <include file="struts-default"></include>
    <package name="default" extends="struts-default">
        <action name="uploadFiles" class="com.usr.fileupload.action.UploadMultipleFiles"
            method="upload">

            <interceptor-ref name="exception" />
            <interceptor-ref name="alias" />
            <interceptor-ref name="servletConfig" />
            <interceptor-ref name="prepare" />
            <interceptor-ref name="i18n" />
            <interceptor-ref name="chain" />
            <interceptor-ref name="debugging" />
            <interceptor-ref name="profiling" />
            <interceptor-ref name="scopedModelDriven" />
            <interceptor-ref name="modelDriven" />
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">500000</param>
                <param name="allowedTypes">image/gif,image/pjpeg,image/png,image/jpg</param>
            </interceptor-ref>
            <interceptor-ref name="checkbox" />
            <interceptor-ref name="staticParams" />
            <interceptor-ref name="actionMappingParams" />
            <interceptor-ref name="params">
                <param name="excludeParams"> dojo\..*,^struts\..*</param>
            </interceptor-ref>
            <interceptor-ref name="conversionError" />
            <interceptor-ref name="validation">
                <param name="excludeMethods"> input,back,cancel,browse</param>
            </interceptor-ref>
            <interceptor-ref name="workflow">
                <param name="excludeMethods"> input,back,cancel,browse</param>
            </interceptor-ref>


            <result name="success">/success.jsp</result>
            <result name="input">/index.jsp</result>
        </action>
    </package>

</struts>   
   
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<h2><s:text name="title"></s:text></h2>
<s:form action="uploadFiles" method="post" enctype="multipart/form-data">
<s:file name="attachment" key="attachment1"></s:file>
<s:file name="attachment" key="attachment2"></s:file>
<s:file name="attachment" key="attachment3"></s:file>
<s:submit key="button"></s:submit>
</s:form>
</body>
</html>

success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<h1>Struts 2 upload multiple files example</h1>
<hr>

<h4>
   File Name : <s:property value="attachmentFileName"/>
</h4>

<h4>
   Content Type : <s:property value="attachmentContentType"/>
</h4>

<h4>
   File : <s:property value="attachment"/>
</h4>
</body>
</html>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.