12:46 pm - Saturday July 22, 2017

struts 1x File Uploading with example program In Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044


Step by Step tutorial on running  your FileUpload Struts application in

Tomcat5.5 with Eclipse IDE

Step – 1  Creating Dynamic web Project

Step – 2 FileUpload STRUTS folder structure

Step – 3  Creating your FileUpload Struts program(Model and Controller)

Step – 4 Creating web.xml for FileUpload struts application

Step – 5 Creating Struts-config.xml

Step – 6 FileUpload Struts view page

Step – 7How to Add server in  FileUpload Struts application

Step – 8OutPut  for  Running  FileUpload Struts application

Step – 1  Creating Dynamic web Project

 SOURCE CODE  

 

 Step – 2 FileUpload STRUTS folder structure


Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

Step – 3  Creating your FileUpload Struts program(Model and Controller)

MyAction.java

package Com.Candidjava;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import javax.servlet.http.*;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class MyAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		MyModel m = (MyModel) form;// creating instance of model class(mymodel)

		FormFile myFile = m.getFile();

		String contentType = myFile.getContentType();
		String fileName = myFile.getFileName();
		int fileSize = myFile.getFileSize();
		byte[] fileData = myFile.getFileData();
		System.out.println("contentType: " + contentType);
		System.out.println("File Name: " + fileName);

		HttpSession session = request.getSession(true);
		session.setAttribute("fileName", fileName);
		System.out.println("File Size: " + fileSize);
		return mapping.findForward("suc");

	}
}

MyModel.java

package Com.Candidjava;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class MyModel extends ActionForm {

	private FormFile file = null;

	public void setFile(FormFile file) {
		this.file = file;
	}

	public FormFile getFile() {
		return this.file;
	}

}

Step – 4 Creating web.xml for FileUpload struts application

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

	<display-name>Struts Blank Application</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-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<taglib>
		<taglib-uri>/tags/struts-html</taglib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>/tags/struts-bean</taglib-uri>
		<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
	</taglib>

</web-app>

Step – 6 FileUpload Struts view page

index.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<!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 bgcolor="pink">

<html:form action="/myActionForm" method="post"
	enctype="multipart/form-data">

	Choose the File to UPLOAD

		<html:file property="file" />

	<html:submit></html:submit>
	<br>
	<br>

</html:form>
</body>
</html>

success.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="pink">
The fileName is:::
<%=session.getAttribute("fileName")%>
</body>
</html>

Step – 7 OutPut  for  Running  FileUpload Struts application

DOWNLOADSOURCE CODE                                                                                                                                                     BACK

Filed in: Z-AStruts Codes

No comments yet.

Leave a Reply