views:

301

answers:

1

Hello,

I first used primefaces FileUpload component and it didn't work. Always gave "HTTP Error". So i thought there is some bug with this component and went to plain old JQuery and tried using uploadify. But still i get the same error. I am using Container Managed Security. Is this the reason for not working properly?

This is my script :-

 $(document).ready(function(){
                       $('#photoInput').uploadify({
                        'script'    : '/Blogger/fileUploadServlet',
                        'uploader'  : './uploadify/uploadify.swf',
                        'cancelImg' : './uploadify/cancel.png',
                        'auto'      : true
                        });

UPDATE

Response Headers:

X-Powered-By Servlet/3.0, JSF/2.0 Server GlassFish v3
Set-Cookie JSESSIONID=a23a36b147ac171f8abbf64406cd; Path=/Blogger
Content-Type text/html;charset=UTF-8
Content-Length 1430 Date Thu, 29 Apr 2010 15:16:12 GMT

Request Headers

Host localhost:8080 User-Agent Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.6) Gecko/20091215 Ubuntu/9.10 (karmic) Firefox/3.5.6 
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language en-us,en;q=0.5 
Accept-Encoding gzip,deflate 
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 
Keep-Alive 300 
Connection keep-alive 
Cookie username=laala; password=panchal; JSESSIONID=a029f59bed8ba0c22fb27a612bf2; treeForm:tree-hi=treeForm:tree:applications; JSESSIONID=962073f17f3d0ebb37536f789b90 Cache-Control max-age=0**

And this is my servlet which is never executed :-

package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
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.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

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

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, FileUploadException {
        PrintWriter out = response.getWriter();
        try {
            System.out.println("Executed!!");
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);

            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
            List /* FileItem */ items = upload.parseRequest(request);

            Iterator e = items.iterator();
            while(e.hasNext()){
                System.out.println(e.next().toString());
            }

        } finally {
            out.close();
        }
    }
}

                      });
A: 

Since you didn't post the HTTP request/response headers, it's a bit shooting in the dark. But a common cause is that the file upload request isn't using the same session while that's required by the webapplication in question. You can easily spot that by a missing cookie in the request header.

To fix this, update the line

'script': '/Blogger/fileUploadServlet',

to

'script': '/Blogger/fileUploadServlet;jsessionid=${pageContext.session.id}',

and retry.

Update: The relative URL might be wrong. What's the absolute URL of the JSF page? What's the absolute URL of the upload servlet? You need to extract the right relative URL from it. Right now you have specified the relative URL with a leading slash / so that it's relative to the domain root, i.e. it would absolutely become http://localhost:8080/Blogger/fileUploadServlet.

You may want to consider to leave this aside and retry with a blank/small playground setup as I've outlined in the "Update" part of this answer and see if it works without all other possibly disturbing factors.

BalusC
Hi Balusc,i edited as you said. After adding jsessionid i still get the error. I am using Glassfish V3. Do i need to make some setting before i can use Flash with Java? Please help me :(
Ankit Rathod
Debug the JS with Firebug. Does it get executed correctly? I'm more interested in request headers sent by uploadify. You can find it in `Net` tab of Firebug. Since you're using JSF, have you ensured that `$('#photoInput')` is the right element ID? It should not be the JSF component ID, but the client ID of the generated HTML component (rightclick page, view source).
BalusC
Yes BalusC, photoInput is the clientId because i have set prependId=false in h:form. Also the component gets the look and feel of Jquery uploadify. Had the id not been correct, it wouln't have got that look and feel. The headers that i sent you were from Net tab only. I don't see any header once i click on browse button of uploadify and select file. All i get is "HTTP - Error". The headers don't get updated in Net tab. What do i do now?
Ankit Rathod