1 Reply Latest reply: Sep 2, 2008 4:43 AM by 843844 RSS

    Uploading a file to server using ajax and struts

    843844
      My problem is i wrote a program to upload a file to the server using Ajax.
      Here iam used Struts and Ajax.
      The problem is when iam uploaded a file from my PC the file is uploading to the server in the upload folder located in the server my system.
      Iam using Tomcat server 5.0
      But when iam trying to access it through other system it is not doing so
      Giving an internal server error i,e 500.
      Iam putting the necessary documents for ur reference.
      Plz help me soon .
      My exact requirement is i have to upload a file to the upload folder located in the server.
      And i have to get the path of that file and display the file path exactly below the browse button from where iam uploaded a file.
      That should be done without page refresh and submit thats y iam used Ajax
      Any help would greatly appreciated
      Thanks and Regards
      Meerasaaheb.



      The action class is FilePathAction


      package actions;
      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 javax.servlet.ServletException;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.*;


      public class FilePathAction extends Action{
      public ActionForward execute(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException
      {

      String contextPath1 = "";
      String uploadDirName="";
      String filepath="";
      System.out.println(contextPath1 );
      String inputfile = request.getParameter("filepath");
      uploadDirName = getServlet().getServletContext().getRealPath("/upload");
      File f=new File(inputfile);
      FileInputStream fis=null;
      FileOutputStream fo=null;
      File f1=new File(uploadDirName+"/"+f.getName());
      fis=new FileInputStream(f);
      fo=new FileOutputStream(f1);

      try
      {
      byte buf[] = new byte[1024*8]; /* declare a 8kB buffer */
      int len = -1;
      while((len = fis.read(buf)) != -1)
      {
      fo.write(buf, 0, len);
      }
      }
      catch(Exception e)
      {
      e.printStackTrace();
      }
      filepath=f1.getAbsolutePath();
      request.setAttribute("filepath", filepath);
      return mapping.findForward("filepath");
      }

      }



      the input jsp is
      filename.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>
      <script type="text/javascript">
      alertflag = false;
      var xmlHttp;

      function startRequest()

      {
      if(alertflag)
      alert("meera");

      xmlHttp=createXmlHttpRequest();
      var inputfile=document.getElementById("filepath").value;
      xmlHttp.open("POST","FilePathAction.do",true);
      xmlHttp.onreadystatechange=handleStateChange;
      xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      xmlHttp.send("filepath="+inputfile);
      }
      function createXmlHttpRequest()
      {
      //For IE
      if(window.ActiveXObject)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      //otherthan IE
      else if(window.XMLHttpRequest)
      {
      xmlHttp=new XMLHttpRequest();
      }
      return xmlHttp;
      }

      //Next is the function that sets up the communication with the server.



      //This function also registers the callback handler, which is handleStateChange. Next is the code for the handler.

      function handleStateChange()
      {
      var message=" ";
      if(xmlHttp.readyState==4)
      {
      if(alertflag)
      alert(xmlHttp.status);
      if(xmlHttp.status==200)
      {
      if(alertflag)
      alert("here");
      document.getElementById("div1").style.visibility = "visible";

      var results=xmlHttp.responseText;


      document.getElementById('div1').innerHTML = results;


      }

      else
      {
      alert("Error loading page"+xmlHttp.status+":"+xmlHttp.statusText);
      }
      }
      }
      </script></head><body><form name="thumbs" enctype="multipart/form-data" method="post" action="">
      <input type="file" name="filepath" id="filepath" onchange="startRequest();"/>
      </form>
      <div id="div1" style="visibility:hidden;">
      </div></body></html>




      The ajax response is catching in a dummy.jsp
      <%=(String)request.getAttribute("filepath")%>



      corresponding action mapping


      <action path="/FilePathAction" type="actions.FilePathAction">
      <forward name="filepath" path="/dummy.jsp"/>
      </action>



      So plz help me to upload a file to the server from any PC.
      Iam searched alot but didnt get any solution.