Fileupload Example in Struts 2.0
Fileupload Example in Struts 2.0
Fileupload Example using Struts 2.0 the following tool are required for run this example
- JDK 1.5
- MyEclipse IDE
- Server Tomcat 6.0
- Struts 2.0 jar file
Directory Structure of FileUpload Example in Struts 2.0 Using MyEclipse IDE
index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>File Upload</title>
</head>
<body> <br/><br/><br/><center>
<div style="height: 200px;width: 500px;background-color: gray;"><br/><br/>
<h1>Single File Upload</h1>
<s:actionerror/>
<s:form action="upload" enctype="multipart/form-data" method="POST">
<s:textfield name="description" label="File Description"/>
<s:file name="attachment" label="Attachment File"/>
<s:submit />
</s:form>
</div></center>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="File">
<result>/index.jsp</result>
</action>
<action name="upload" class="org.r4r.FileUploadAction" method="upload">
<interceptor-ref name="fileUpload">
<param name="maximumSize">500000</param>
<param name="allowedTypes"> image/gif,image/pjpeg,image/png </param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
FileUploadAction.java
package org.r4r;
import java.io.File;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private File attachment;
private String attachmentFileName;
private String attachmentContentType;
private String description;
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";
}
public File getAttachment() {
return attachment;
}
public void setAttachment(File attachment) {
this.attachment = attachment;
}
public String getAttachmentFileName() {
return attachmentFileName;
}
public void setAttachmentFileName(String attachmentFileName) {
this.attachmentFileName = attachmentFileName;
}
public String getAttachmentContentType() {
return attachmentContentType;
}
public void setAttachmentContentType(String attachmentContentType) {
this.attachmentContentType = attachmentContentType;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
You have uploaded the following file.
<hr>
Description : <s:property value="description"/> <br>
ContentType : <s:property value="attachmentContentType"/> <br>
File : <s:property value="attachment"/><br/>
<img alt="ss" src='images/<s:property value="attachmentFileName"/>'>
<br/>
</body>
</html>
Output
