File Upload (Using Struts)
Project 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Front Controller</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Front Controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>http://jakarta.apache.org/struts/tags-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
struts-config.xml
<!DOCTYPE struts-config>
<struts-config>
<form-beans>
<form-bean name="formbean" type="FileForm" />
</form-beans>
<action-mappings>
<action path="/file" type="FileAction" name="formbean" input="/index.jsp" validate="true" scope="session">
<forward name="done" path="/index.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
index.jsp
<%@ 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">
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
<html:form action="/file" enctype="multipart/form-data" method="POST">
<html:file property="filename" ></html:file>
<input type="submit" name="upload" value="upload" ><html:errors property="er"/>
</html:form>
</body>
</html:html>
FileForm.java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class FileForm extends ActionForm
{
private FormFile filename;
public FormFile getFilename()
{
return filename;
}
public void setFilename(FormFile filename)
{
this.filename = filename;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
if(filename.getFileSize()>5242880)
{
errors.add("er",new ActionError("error"));
}
return errors;
}
}
FileAction.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.upload.FormFile;
import java.io.*;
public class FileAction extends Action
{
private FileForm f;
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest req, HttpServletResponse res)throws Exception
{
f = (FileForm)form;
FormFile nf = f.getFilename();
File file = new File("e:\\"+nf.getFileName());
FileOutputStream out = new FileOutputStream(file);
out.write(nf.getFileData());
out.close();
return mapping.findForward("done");
}
}
error = File size must be less than 5MB.
Required Dependencies
http://www.4shared.com/rar/kNCxn8sP/FileUploadDependencies.html
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Front Controller</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Front Controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>http://jakarta.apache.org/struts/tags-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
struts-config.xml
<!DOCTYPE struts-config>
<struts-config>
<form-beans>
<form-bean name="formbean" type="FileForm" />
</form-beans>
<action-mappings>
<action path="/file" type="FileAction" name="formbean" input="/index.jsp" validate="true" scope="session">
<forward name="done" path="/index.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/>
</struts-config>
index.jsp
<%@ 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">
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Upload</title>
</head>
<body>
<html:form action="/file" enctype="multipart/form-data" method="POST">
<html:file property="filename" ></html:file>
<input type="submit" name="upload" value="upload" ><html:errors property="er"/>
</html:form>
</body>
</html:html>
FileForm.java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class FileForm extends ActionForm
{
private FormFile filename;
public FormFile getFilename()
{
return filename;
}
public void setFilename(FormFile filename)
{
this.filename = filename;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
if(filename.getFileSize()>5242880)
{
errors.add("er",new ActionError("error"));
}
return errors;
}
}
FileAction.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.Action;
import org.apache.struts.upload.FormFile;
import java.io.*;
public class FileAction extends Action
{
private FileForm f;
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest req, HttpServletResponse res)throws Exception
{
f = (FileForm)form;
FormFile nf = f.getFilename();
File file = new File("e:\\"+nf.getFileName());
FileOutputStream out = new FileOutputStream(file);
out.write(nf.getFileData());
out.close();
return mapping.findForward("done");
}
}
ApplicationResources.properties
error = File size must be less than 5MB.
Required Dependencies
http://www.4shared.com/rar/kNCxn8sP/FileUploadDependencies.html

Comments
Post a Comment