Sunday, May 9, 2010

Struts Application-File Upload and Download

 
Struts Application-File Upload
Aim:
     To browse the file from the hard disc and save the file into hard disk using file in  
          struts framework.   
Procedure:

            1). Createing actionform

            2). Creating action

            3). Creating jsp file

            4). Configure Struts-config.xml
Step_1:

            Creating actionform

I think you are very familiar how to create actionform.

Create actionform (I have used actionform as fileForm)

import org.apache.struts.upload.FormFile;

Include

Private FormFile thefile;

Create setter and getter method for private FormFile thefile;



package actionforms;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
public class fileForm extends org.apache.struts.action.ActionForm
{

private FormFile thefile;

public FormFile getThefile()
{
return thefile;
}
public void setThefile(FormFile thefile)
{
this.thefile = thefile;
}

}

Step_2:

            Creating action

            Create action usually (I have used action as fileAction)

            Include following coding.

package actions;

import actionforms.fileForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.upload.FormFile;
public class fileAction extends org.apache.struts.action.Action
{
   
   
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        fileForm sform=(fileForm)form;
       
        FormFile myFile=sform.getThefile();
       
        String filename=myFile.getFileName();
       
        String contenttype=myFile.getContentType();
       
        int filesize=myFile.getFileSize();
       
        String filepath=getServlet().getServletContext().getRealPath("/")+"jsp";
       
        byte[] filedata=myFile.getFileData();
       
        if(!filename.equals(""))
        {
            File fileToCreate = new File(filepath,filename);
                         
                    if(!fileToCreate.exists())
                    {
                      FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
                      fileOutStream.write(myFile.getFileData());
                      fileOutStream.flush();
                      fileOutStream.close();

                    }
        }
        request.setAttribute("filename", filename);
       
        return mapping.findForward("success");
       
    }
}





Step_3:

Creating jsp (I have used as Home.jsp)

< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >

< html >
< head > < title>My JSP 'sureshform.jsp' starting page < /title >
< /head >
< body >
< html:form action="/fileaction" method="post" enctype="multipart/form-data" >

< html:file property="thefile"> < /html:file >
< html:submit > < /html:submit >

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



Creating jsp for download(I have used as download.jsp)

< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< %@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< %@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >


< jsp:directive.page import="java.util.Vector"/ >

< html >


< body >
< %

String filename=(String) request.getAttribute("filename");
out.println(filename);
% >
< a href="jsp/<%= filename %>" > Download
< /body >

< /html >


Step_4:

Configure Struts-config.xml



Step_5:

Run Home.jsp


Conclusion:

                   From this post we have understood how to upload the file. and also how to download from

                  uploaded file.

                  Any Query , post comment.


No comments:

Post a Comment