0

I am trying to upload a file to WebLogic server using servlets. This is the doPost method:

    public void doPost(HttpServletRequest request, 
               HttpServletResponse response)
              throws ServletException, java.io.IOException 
{

    boolean isMultipart;
    String filePath=null;
    int maxFileSize = 2 * 1024 * 1024;
    int maxMemSize = 1024 * 1024;
    File file ;

      // Check that we have a file upload request
    isMultipart = ServletFileUpload.isMultipartContent(request);
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter( );
    if( !isMultipart )
    {
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Servlet upload</title>");  
         out.println("</head>");
         out.println("<body>");
         out.println("<p>No file uploaded</p>"); 
         out.println("</body>");
         out.println("</html>");
         return;
      }
      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(2 * 1024 * 1024);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File("c:\\temp"));

      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory);
      // maximum file size to be uploaded.
      upload.setSizeMax( maxFileSize );

      try{ 
      // Parse the request to get file items.
      List fileItems = upload.parseRequest(request);

      // Process the uploaded file items
      Iterator i = fileItems.iterator();

      out.println("<html>");
      out.println("<head>");
      out.println("<title>Servlet upload</title>");  
      out.println("</head>");
      out.println("<body>");

      while ( i.hasNext () ) 
      {
         FileItem fi = (FileItem)i.next();
         if ( !fi.isFormField () )      
         {
            // Get the uploaded file parameters
            String fieldName = fi.getFieldName();
            String fileName = fi.getName();
                ServletContext context = getServletContext();  
                Connection conn = (Connection)context.getAttribute("DBCON");
                PreparedStatement stmt=null;

                try 
                {
                    stmt=conn.prepareStatement("INSERT INTO emp VALUES(?, ?, ?)");
                    out.println("Created the statement!");
                } 
                catch (SQLException e) 
                {
                    out.println("Unable to create prepared statement because: "+e.getMessage());
                }

                WriteToDB ob=new WriteToDB();
                out.println("CWriting to DB!");
                ob.process("C:\\JDeveloper\\mywork\\App\\Project1\\data" + "\\" + fileName, stmt, out);
                out.println("Wrote to DB!");
            }
         }
      } catch (FileUploadException e) {
    }
    out.println("</body>");
      out.println("</html>");
   }

And this is, as I've read, the important part of web.xml:

<context-param> 
<description>Location to store uploaded file</description> 
<param-name>file-upload</param-name> 
<param-value>
C:\JDeveloper\mywork\App\Project1\data
 </param-value> 

I am using JDeveloper.

I have a standard upload HTML page with form data. Everything is fine, except that the file is not to be found at the stored location, and it throws that exception. Please help, thank you.

Vedavyas Bhat
  • 2,068
  • 1
  • 21
  • 31
  • ... What exception? `FileUploadException`? Perhaps including the stack trace might aid in diagnosing the problem. Just a hunch. – Dave Newton Jan 17 '14 at 06:31
  • The process() function is throwing (and handling) a file not found exception. This is the output on the browser: Created the statement! CWriting to DB! Inside process! Error message: C:\JDeveloper\mywork\App\Project1\data\EMP.xls (The system cannot find the file specified) null Wrote to DB! – Vedavyas Bhat Jan 17 '14 at 06:34
  • The problem seems obvious then. – Dave Newton Jan 17 '14 at 06:36
  • And I checked the location too, there is no file there. Why is the file not getting uploaded? – Vedavyas Bhat Jan 17 '14 at 06:36

0 Answers0