fileupload.jsp
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@page import="java.util.Iterator"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="false"%>
<%!
String Path=null;
HttpSession session;
String basePath =null;
%> <%
Path = request.getContextPath();
basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+Path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stats Section </title>
</head>
<body>
<html:form action="/FileUploadControl" method="post" enctype="multipart/form-data" styleId="stateformid">
<html:file property="statsfile">Upload</html:file>
<html:submit>Add</html:submit>
</html:form>
</body>
</html>
FileUploadControl.java
package action;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import bean.AddFileBean;
import dao.DaoFactory;
import dao.SqlDao;
public class FileUploadControl extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
HttpSession session
=
request.getSession(false);
ServletContext context
= getServlet().getServletContext();
String excel_location
=context.getInitParameter("excelpath");
excel_location
=request.getRealPath(excel_location);
excel_location
=excel_location+"/";
DaoFactory sql
=
new SqlDao();
AddFileBean addfile
=
new AddFileBean();
int i
=
0;
int id
=
0;
AddFileBean addfile1
= (AddFileBean)form;
FormFile statsfile
=
addfile1.getStatsfile();
String fileName1
= statsfile.getFileName();
if(!fileName1.equals(""))
{
File fileToCreate = null;
if(fileName1.equals(""))
{
fileName1
=
"";
}
else
{
fileToCreate = new File(excel_location, statsfile.getFileName());
FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
fileOutStream.write(statsfile.getFileData());
fileOutStream.flush();
fileOutStream.close();
}
excel_location=excel_location+statsfile.getFileName();
String excel_location1 = excel_location.replace("\\","/");
sql.AddData(excel_location1);
}
else
{
i = 1 ;
}
if(i!=1)
return mapping.findForward("success");
else
return mapping.findForward("error");
}}
AddFileBean.java
package bean;
import org.apache.struts.upload.FormFile;
public class AddFileBean extends org.apache.struts.validator.ValidatorForm
{
private FormFile statsfile;
public FormFile getStatsfile()
{
return statsfile;
}
public void setStatsfile(FormFile statsfile)
{
this.statsfile = statsfile;
}
}
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:jsp="http://java.sun.com/xml/ns/javaee/jsp" 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">
<context-param>
<param-name>excelpath</param-name>
<param-value>/Excel</param-value>
</context-param>
<display-name>nccp</display-name>
<servlet>
<servlet-name>action</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>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<description></description>
<display-name>FileUploadControl</display-name>
<servlet-name>FileUploadControl</servlet-name>
<servlet-class>Action.FileUploadControl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadControl</servlet-name>
<url-pattern>/FileUploadControl</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/fileupload.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="AddFileBean"
type="bean.AddFileBean"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<action input="fileupload.jsp" name="AddFileBean" path="/FileUploadControl" scope="request" type="action.FileUploadControl">
<forward name="success" path="/success.jsp" />
<forward name="error" path="/error.jsp" />
</action>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="action/ApplicationResource"> </message-resources>
<!-- ========================= Tiles plugin ===============================-->
<!--
This plugin initialize Tiles definitio n factory. This later can takes some
parameters explained here after. The plugin first read parameters from
web.xml, thenoverload them with parameters defined here. All parameters
are optional.
The plugin should be declared in each struts-config file.
- definitions-config: (optional)
Specify configuration file names. There can be several comma
separated file names (default: ?? )
- moduleAware: (optional - struts1.1)
Specify if the Tiles definition factory is module aware. If true
(default), there will be one factory for each Struts module.
If false, there will be one common factory for all module. In this
later case, it is still needed to declare one plugin per module.
The factory will be initialized with parameters found in the first
initialized plugin (generally the one associated with the default
module).
true : One factory per module. (default)
false : one single shared factory for all modules
- definitions-parser-validate: (optional)
Specify if xml parser should validate the Tiles configuration file.
true : validate. DTD should be specified in file header (default)
false : no validation
Paths found in Tiles definitions are relative to the main context.
-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>