FileUpload in Struts2.0 Framework

FileUpload in Struts2.0 Framework

Previous Home Next

 

The FileUpload interceptor provide the facility to upload file on the server . This interceptor upload different type of file and also provide the facility to give the size of the file through the configuration file.there are only two things you need to do.

First, use the file tag in a form on your JSP.

<s:file name="attachment" label="Attachment "/>

Second, create an action class with three properties. The properties must be named according to these patterns:

• [inputName] File

• [inputName]FileName

• [inputName]ContentType 

File upload interceptor is responsible for uploading file on the server and as include the default stack. the interceptor set the two important property. which are following maximumSize. The maximum size (in bytes) of the uploaded file. The default is about 2MB.allowedTypes. A comma-separated list of allowable content types.For example, the following action imposes a size limit and the type of the uploaded file. Only files up to 1,000,000 bytes in size and JPEG, GIF, and PNG files can be uploaded.

 

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





Previous Home Next