Wednesday 8 February 2012

Struts2 File Upload

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 File Upload Application
description=File Description
attachment=Attachement File
button=Submit


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="fileUpload" method="post" enctype="multipart/form-data">
<s:textfield name="description" key="description"></s:textfield>
<s:file name="attachment" key="attachment"></s:file>
<s:submit key="button"></s:submit>
</s:form>
</body>
</html>

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

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport{
private String description;
private File attachment;
private String attachmentContentType;
private String attachmentFileName;
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public File getAttachment() {
    return attachment;
}
public void setAttachment(File attachment) {
    this.attachment = attachment;
}
public String getAttachmentContentType() {
    return attachmentContentType;
}
public void setAttachmentContentType(String attachmentContentType) {
    this.attachmentContentType = attachmentContentType;
}
public String getAttachmentFileName() {
    return attachmentFileName;
}
public void setAttachmentFileName(String attachmentFileName) {
    this.attachmentFileName = attachmentFileName;
}
public String execute(){
    return "success";
}
public String upload() throws Exception {
    System.out.println(description);
    ServletContext servletContext = ServletActionContext.getServletContext();
    if (attachment != null) {
        String dataDir = servletContext.getRealPath("/images");
        File savedFile = new File(dataDir, attachmentFileName);
        attachment.renameTo(savedFile);
        }
    return "success";
    }
   
}



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>
 File uploaded successfully...!!!
 <hr>
Description:<s:property value="description"/>
ContentType : <s:property value="attachmentContentType"/> <br>
File : <s:property value="attachment"/><br/>
<img alt="ss" src='images/<s:property value="attachmentFileName"/>'>
<br/>
</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>
<include file="struts-default"></include>
<package name="default" extends="struts-default">
<action name="fileUpload" class="com.usr.fileupload.action.FileUpload"
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</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>  
    


No comments:

Post a Comment

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