• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Hello friends as you know i’mkunal sachdevarite now i’m also working on creating awebsite for jobs seeker & jobs provider using Struts framework which requiresfileuploading & downloading of Resume, so here is the code for you which allows you toupload & download any type of file.
Before I Explain you the code, let me give u brief introduction of struts framework first:-
Struts is a free open-source framework for creating Servlet/JSP based web applicationsbased on Model-View-Controller (MVC) architecture. The Model represents the businessor database code, the View represents the page design code, and the Controller representsthe navigational code. The Struts framework is designed to help developers create webapplications that utilize a MVC architecture.
you can use any IDE to develop your struts application like NetBeans, Eclipse or Weblogic.First of all create a jsp named
FileUpload<html:html><head><title>Struts File Upload</title><html:base/></head><body><html:form action=”/fileupload” method=”post” enctype=”multipart/form-data”><font size=”5″>File Upload on Server</font><font color=”red”><html:errors/></font>File Name<html:file property=”theFile”/><center><html:submit>Upload File</html:submit></center></html:form></body></html:html>
 Now since we have used html tag library so you will have to addtaglib uri of html in case of Netbeans.This jsp will have file type property from where u can browse the file on clicking submit theaction fileupload will be called. Now create a Bean package named beans & then create a class bean named
StrutsUploadAndSaveForm
which will extend ActionForm class
package beans;import org.apache.struts.action.*;import org.apache.struts.upload.FormFile;
 
public class StrutsUploadAndSaveForm extends ActionForm{private FormFile theFile;public FormFile getTheFile() {return theFile;}public void setTheFile(FormFile theFile) {this.theFile = theFile;}}
in the above code getTheFile function Returns the File &setTheFile function set the file whose parameters are passes. Now create action package named action and create a action class named
StrutsUploadAndSaveAction
which extends Action class
package action;import bean.StrutsUploadAndSaveForm;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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 java.io.*;public class StrutsUploadAndSaveAction extends Action{public ActionForward execute(ActionMapping mapping,ActionFormform,HttpServletRequest request, HttpServletResponse response) throws Exception{StrutsUploadAndSaveForm myForm = (StrutsUploadAndSaveForm)form;// Process the FormFileFormFile myFile = myForm.getTheFile();String contentType = myFile.getContentType();//Get the file nameString fileName = myFile.getFileName();byte[] fileData = myFile.getFileData();//Get the servers upload directory real path nameString filePath = getServlet().getServletContext().getRealPath(”/”) +”upload”;/* Save file on the server */if(!fileName.equals(”")){System.out.println(”Server path:” +filePath);//Create fileFile fileToCreate = new File(filePath, fileName);//If file does not exists create fileif(!fileToCreate.exists())
 
{FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);fileOutStream.write(myFile.getFileData());fileOutStream.flush();fileOutStream.close();}
 
}//Set file name to the request objectrequest.setAttribute(”fileName”,fileName);return mapping.findForward(”success”);}}
/* This action class first takes the file which is set in the bean. then its actual path is stored in thevariable filepath.then if the filepath is not empty, a new file with same name is created inside realpath of servlet or  jsp/uploadif the filepath is empty than fileOutStream is flushed.& in the end name of the file is set in the request scope such that the same file can bedownloaded latter. Now enter the Code in
Struts Config
such that their is right mapping of the code.
<struts-config><form-beans><form-bean name=”FileUpload” type=”beans.StrutsUploadAndSaveForm”/>
 
</form-beans><action-mappings><action path=”/fileUpload” type=”action.StrutsUploadAndSaveAction”name=”FileUpload” scope=”request” validate=”true” input=”/FileUploadAndSave.jsp”><forward name=”success” path=”/downloadfile.jsp”/></action></action-mappings></struts-config>
 Now create a jsp named
downloadfile.jsp<html><head><title>Success</title></head><body><%String fileName=(String)request.getAttribute(”fileName”);%>
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...