开发者

JQuery uploadify plugin not working

开发者 https://www.devze.com 2022-12-28 09:21 出处:网络
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.

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=96207开发者_如何学编程3f17f3d0ebb37536f789b90 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();
        }
    }
}

                      });


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消