tags:

views:

262

answers:

2

Hello, Why is this code not working? I always get size() = 0, whenever i upload file.

xhtml file :-

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"&gt;
    <h:head>
        <title>Abc</title>

    </h:head>
    <h:body>
        <center>

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

        </center>
    </h:body>
</html>

This is my servlet :-

package servlets;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


@WebServlet(name="uploadFile", urlPatterns={"/uploadFile"})
public class uploadFile extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileUploadException {
        System.out.println("executed!");
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        System.out.println(items.size());
         for (FileItem item : items) {
                if (!item.isFormField()) {
                    System.out.println("Name: " + item.getName());
                    System.out.println("Size: " + item.getSize());
                    System.out.println("Type: " + item.getContentType());
                }
            }
    } 


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (FileUploadException ex) {
            ex.printStackTrace();
        }
    } 


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (FileUploadException ex) {
            ex.printStackTrace();
        }
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

Update:

1) If i just add name = "something" to input type = "file", then it works fine. What logic can be there behind this?

2) If i use h:form instead of form, then it doesn't work (even my actionListener method doesn't get executed). What reason is there behind this? I read that h:form is not designed to handle multipart/form-data request. Is it true?

+2  A: 

1) If i just add name = "something" to input type = "file, then it works fine. What logic can be there behind this :d?

The name is required to distinguish the submitted value from other values - otherwise, how would you identify the parameter in the request?

2) If i use h:form instead of form, then it doesn't work (even my actionListener method doesn't get executed). What reason is there behind this? I read that h:form is not designed to handle multipart/form-data request. Is it true?

You're trying to mix JSF controls and plain HTML elements. If you don't hit the JSF servlet, don't expect the JSF lifecycle to be executed. JSF forms can support file upload - many JSF component libraries provide such controls.

McDowell
+1  A: 

I see that you're using Servlet 3.0. It has adopted the Commons FileUpload under the hoods behind the HttpServletRequest#getParts() method. You can read here how you are supposed to use it.

Since JSF 2.0 eases creating custom components more and Tomahawk isn't JSF 2.0 ready yet (you would normally choose t:inputFileUpload to have a JSF file upload component), you can also consider to create a custom file upload component yourself. This is outlined in detail in this article.


That said, the HTML <center> element is deprecated since 1998. You want to use the CSS style margin: 0 auto; on the containing block element instead.

BalusC
Great! answer BalusC. Very informative. From next time, i will take care about not using <center>
Ankit Rathod
Hi BalusC, are you machine? or human? How can you write so long articles with so much fine detailed information, answer so many questions on stackoverflow and earn 54k points? What do you get by doing so much for free? and yet make projects for your clients and earn? How many hours do you sleep?
Ankit Rathod