Samiksha Manglani
Samiksha Manglani

Reputation: 31

getPart() returns null on uploading a file using servlets i have included @MultipartConfig but still it returns null

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import com.oreilly.servlet.*;
import javax.servlet.http.Part;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.*;
@MultipartConfig
@WebServlet("upload")
public class FileUploadServlet extends HttpServlet
{
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
         Part filePart =req.getPart("fileName");
         String realPath = getServletContext().getRealPath("file");
        MultipartRequest mpr = new MultipartRequest(req,realPath,500*1024*1024);
        InputStream inputStream = null; // input stream of the upload file

        // obtains the upload file part in this multipart request



        if (filePart != null) {
            // prints out some information for debugging
            out.println(filePart.getName());
            out.println(filePart.getSize());
            out.println(filePart.getContentType());

            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }
        out.println("<html><body>");
        //out.println(fileName);
        out.println("File uploaded successfully");
        out.println("</body></html>");

    }
}

and the html is

<html>
<head>
<title>form</title>
<body>
<h2>Upload Contribution</h2>
          <br><br>

          <p>Contribution Domain: <input type="text" name="contdomain">
          <br>
          <br>
          Contribution Name: <input type="text" name="contname">
          <br>
          <br>
  <form method="post" action="upload" name="submit" enctype="multipart/form-data">
  <input type="file" name="fileName"><br /><br />
  <input type="submit" name="submit" value="Submit">
</form>
</head>
</body>
</html>

and the web.xml is

<web-app>
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
</web-app>

kindly help me to resolve this problem... all where i have searched it asks to add @MultipartConfig but adding it still doesnt give the result.

i need the file name so that i cud add the file to database

Upvotes: 3

Views: 8945

Answers (1)

Shota
Shota

Reputation: 7360

The problem is with Servlet declaration, you can remove <servlet></servlet> tag from web.xml file (with everything what's inside), and add the following proper annotation at the top of your Servlet class, so here is full version, which works on my machine:

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.Part;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.*;
@MultipartConfig

@WebServlet(
        name = "FileUploadServlet",
        urlPatterns = { "/upload"},
        loadOnStartup = 1
)
public class FileUploadServlet extends HttpServlet
{
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        Part filePart =req.getPart("fileName");
        InputStream inputStream = filePart.getInputStream();
    }
}

The HTML:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>App</title>
</head>
<body>

<form method="post" action="upload" name="submit" enctype="multipart/form-data">
    <input type="file" name="fileName"><br /><br />
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>App</display-name>

</web-app>

Upvotes: 2

Related Questions