Unable to get value from form fields when using enctype = multipart / form-data: java servlets

advertisements

I have written this to upload an image in the specified folder and store the path in database, but when i click on add a blank page is displayed, I have already created all the folders in webcontent.I am using tomcat server:

My upload.jsp code looks something like this:

        <form action="Add" method=post enctype="multipart/form-data">
            <p>Book name:  <input type="text" name="bname" required/></p>
            <p>Price:<input type="text" name="bprice" required/></p>
            <p>Quantity:<input type="text" name="bqty" required/></p>
            <p>Image: <input type="file" name="file" required/></p>
            <p>Course: <select name="course">
            <option>course 1</option>
            <option>course 2</option> <!-- Some more options-->
            <input type="submit" value="Add" name="Submit"/></p>
        </form>

The Add.java servlet code is:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out=response.getWriter();

    String bname=request.getParameter("bname");
    String bprice=request.getParameter("bprice");
    String bqty=request.getParameter("bqty");
    String path="images";
    String file=request.getParameter("file");
    String course=request.getParameter("course");
    if(course.equals("course 1"))
    {
        path="images/folder1";
    }
    else if(course.equals("course 2"))
    {
        path="images/folder2";
    }
    else
    {
        path="images";
    }
    MultipartRequest m=new MultipartRequest(request,path);
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
       Connection con=DriverManager.getConnection("",user,pass);

        PreparedStatement ps=con.prepareStatement("insert into product(bname,course,price,qty,path) values (?,?,?,?,?)");
        ps.setString(1,bname);
        ps.setString(2,course);
        ps.setString(3,bprice);
        ps.setString(4,bqty);
        ps.setString(5,path+file);

        ps.executeUpdate();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    out.print("<p>Product added!</p>");

    RequestDispatcher rd=request.getRequestDispatcher("upload.jsp");
    rd.include(request, response);

}

I get NullPointer exception. I read somewhere that instead of using request.getParameter() I shoud use m.getParameter()

I did that and it worked, But that won't solve my problem because my code determines the path based on the value of course from the form.

And also I need to get the filename of the file uploaded, right now I am doing this like this: String file=request.getParameter("file");

I was trying some sample code and when I used m.getParameter() I managed to get values of all fields except file(I want that coz i want to store the img path in the DB). When I don't use enctype=multipart/form-data everything works fine (I also get the filename) except the error that content is multipart(That's obvious I know).


ServletRequest#getParameter works only for application/x-www-form-urlencoded data (this encoding is used by default if you don't specify enctype attribute on your form). You can read more information about form content types here.

From the line

MultipartRequest m=new MultipartRequest(request,path);

I am assuming you are using the com.oreilly.servlet library.

In this case, using MultipartRequest#getParameter is a correct way to get the values of fields. In order to get the name of the uploaded file, you can use MultipartRequest#getFilesystemName.

my code determines the path based on the value of course from the form

I am afraid that you won't be able to do this in a clear way with com.oreilly.servlet library. What you could do is move the file by yourself like this:

m.getFile("file").renameTo(new File("newPath"));

Alternatively, you can consider using some other library for dealing with multipart data, such as Apache Commons FileUpload.