Uploadify 插件不调用 Java Servlet [英] Uploadify plugin doesn't call Java Servlet

查看:22
本文介绍了Uploadify 插件不调用 Java Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 Uploadify Flash 插件而不是标准的 HTML UI.
并遇到了下一个问题:

当我单击上传文件"链接时,会显示进度并显示已完成"状态,但实际上 - 它没有发生任何事情,没有从后端调用 Java Servlet.

更早的:

<代码>

提供 Uploadify 插件后,UI 现在看起来像:

插件部分(配置):

 <脚本>...oScript.text+= "$j('#uploadify').uploadify({";oScript.text+= "'uploader' : 'kne-portlets/js/lib/uploadify/scripts/uploadify.swf',";oScript.text+= "'script' : '<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";oScript.text+= "'cancelImg': 'kne-portlets/js/lib/uploadify/cancel.png',";oScript.text+= "'文件夹':'<%= 解码字符串 %>',";oScript.text+= "'queueID' : 'fileQueue',";oScript.text+= "'auto': false,";oScript.text+= "'multi' : false,";//oScript.text+= "'sizeLimit' : 1000";oScript.text+= "});";oScript.text+= "});";...

这里的'scripts'参数指向后端的Java Servlet

<%= decodedString %> 为文件夹路径,值为\file-srvdemo

上传部分:

<a href="javascript:$j('#uploadify').uploadifyUpload();">上传文件</a>

我的错在哪里?

插件配置中的'Script'参数指向后端的Java Servlet,就完成了,但是没有触发Servlet.

错误,当脚本"参数不正确时:http://img190.imageshack.us/i/errormm.png/

感谢您的帮助.

解决方案

这可能有很多可能的原因(另请参阅我发布的评论).

  • 未加载外部 JS.
  • JS 代码在语法/逻辑上无效.
  • 请求网址无效.
  • Servlet 根本没有映射.
  • Servlet 映射到错误的 url-pattern.
  • Servlet 无法启动/初始化.

根据现有信息很难确定根本原因.

正如您提到在 FireBug 的Net"选项卡中没有看到任何请求被触发,我认为 JS 代码在语法上/逻辑上是无效的.右键单击页面并双重验证生成/打印的 JS 代码.

更新:我试图重现您的问题.

  1. 我下载了 jquery.uploadify-v2.1.0 (MIT),将其解压并将全部内容放在 Eclipse 中我的(空)playground web 项目的 /WebContent/uploadify 文件夹中.

  2. 我创建了一个 /WebContent/upload.jsp 文件如下:

    <html lang="zh-cn"><头><title>上传测试</title><script src="uploadify/jquery-1.3.2.min.js"></script><script src="uploadify/swfobject.js"></script><script src="uploadify/jquery.uploadify.v2.1.0.min.js"></script><script type="text/javascript">$(document).ready(function() {$('#uploadify').uploadify({'上传者': '上传/上传.swf','脚本':'上传Servlet','文件夹':'/上传','cancelImg': 'uploadify/cancel.png'});$('#upload').click(function() {$('#uploadify').uploadifyUpload();返回假;});});<身体><input id="uploadify" type="file"><a id="upload" href="#">上传</a>

  3. 我在 的帮助下创建了一个 com.example.UploadServlet 如下Apache Commons FileUpload(刚刚放置commons-fileupload-1.2.1.jarcommons-io-1.4.jar/WEB-INF/lib):

    package com.example;导入 java.io.IOException;导入 java.util.List;导入 javax.servlet.ServletException;导入 javax.servlet.http.HttpServlet;导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse;导入 org.apache.commons.fileupload.FileItem;导入 org.apache.commons.fileupload.disk.DiskFileItemFactory;导入 org.apache.commons.fileupload.servlet.ServletFileUpload;公共类 UploadServlet 扩展了 HttpServlet {protected void doPost(HttpServletRequest 请求,HttpServletResponse 响应)抛出 ServletException、IOException{System.out.println("UploadServlet 被调用.这里是所有上传的文件:");尝试 {列表<文件项>items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);for (FileItem item : items) {如果 (!item.isFormField()) {System.out.println("名称:" + item.getName());System.out.println("尺寸:" + item.getSize());System.out.println("类型:" + item.getContentType());}}} 捕获(异常 e){抛出新的 ServletException(e);}}}

  4. 我在 web.xml 中注册了 com.example.UploadServlet 如下:

    <servlet-name>uploadServlet</servlet-name><servlet-class>com.example.UploadServlet</servlet-class></servlet><servlet-mapping><servlet-name>uploadServlet</servlet-name><url-pattern>/uploadServlet</url-pattern></servlet-mapping>

  5. 我部署了项目,启动了服务器,去了http://localhost:8080/Playground/upload.jsp,从我的下载文件夹中随机选择一个大文件,点击 Upload 链接,看到上传百分比计数器增长到 100%,我终于在标准输出中看到以下内容:

    <前>调用 UploadServlet.以下是所有上传的文件:名称: glassfish-v3-windows.exe尺寸:50402555类型:应用程序/八位字节流

很抱歉,我无法重现您的问题.至少,以上信息应该可以帮助您新鲜"开始.希望有帮助.

更新:根据评论,过滤器期望它使用相同的会话.好的,您可以通过更改来轻松做到这一点

'<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";

'<%= request.getContextPath() %>/uploadFile;jsessionid=${pageContext.session.id}?portletId=${portletId}&remoteFolder=<%= decodedString %>',";

i just started using Uploadify flash plugin instead of standard HTML UI.
And met the next problem:

when I click "Upload Files" link,that progress is shown and "completed" status is appeared, but in reality - it didn't happened anything,Java Servlet isn't called from backend.

There is upload servlet and uploading performed next way earlier:

< form enctype="multipart/form-data" method="post" target="uploadFrame"
action="<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=${remoteFolder}">...

After providing Uploadify plugin, UI now looks like:

plugin part(configuration):

    <script>
...  
         oScript.text+= "$j('#uploadify').uploadify({";
      oScript.text+= "'uploader' : 'kne-portlets/js/lib/uploadify/scripts/uploadify.swf',";
      oScript.text+= "'script'   : '<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";
      oScript.text+= "'cancelImg': 'kne-portlets/js/lib/uploadify/cancel.png',";
      oScript.text+= "'folder'   : '<%= decodedString %>',";
      oScript.text+= "'queueID'  : 'fileQueue',";
      oScript.text+= "'auto'     : false,";
      oScript.text+= "'multi'    : false,";
      //oScript.text+= "'sizeLimit' : 1000";
      oScript.text+= "});";
      oScript.text+= "});"; 
...   
</script>

'scripts' parameter here points to Java Servlet on backend

<%= decodedString %> is folder path, which value is \file-srvdemo

part for uploading:

<input type="file" name="uploadify" id="uploadify" />
<a href="javascript:$j('#uploadify').uploadifyUpload();">Upload Files</a>

Where is my fault?

'Script' param in plugin config points to Java Servlet on backend and it's done,but Servlet isn't triggered.

error, when 'script' param isn't correct:http://img190.imageshack.us/i/errormm.png/

Thank you for assistance.

解决方案

This can have a lot of possible causes (also see the comments I posted).

  • External JS is not loaded.
  • JS code is syntactically/logically invalid.
  • Request URL is invalid.
  • Servlet is not mapped at all.
  • Servlet is mapped on wrong url-pattern.
  • Servlet failed to start/init.

It's hard to naildown the root cause based on the as far given information.

As you mentioned that you didn't see any request been fired in the "Net" tab of FireBug, I think that the JS code is simply syntactically/logically invalid. Rightclick page and doubleverify generated/printed JS code.

Update: I tried to reproduce your problem.

  1. I downloaded jquery.uploadify-v2.1.0 (MIT), extracted it and put the entire contents in the /WebContent/uploadify folder of my (empty) playground web project in Eclipse.

  2. I created a /WebContent/upload.jsp file as follows:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Uploadify test</title>
            <script src="uploadify/jquery-1.3.2.min.js"></script>
            <script src="uploadify/swfobject.js"></script>
            <script src="uploadify/jquery.uploadify.v2.1.0.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#uploadify').uploadify({
                        'uploader': 'uploadify/uploadify.swf',
                        'script': 'uploadServlet',
                        'folder': '/uploads',
                        'cancelImg': 'uploadify/cancel.png'
                    });
                    $('#upload').click(function() {
                        $('#uploadify').uploadifyUpload();
                        return false;
                    });
                });
            </script>
        </head>
        <body>
            <input id="uploadify" type="file">
            <a id="upload" href="#">Upload</a>
        </body>
    </html>
    

  3. I created a com.example.UploadServlet as follows with little help of Apache Commons FileUpload (just placed commons-fileupload-1.2.1.jar and commons-io-1.4.jar in /WEB-INF/lib):

    package com.example;
    
    import java.io.IOException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    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.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    
    public class UploadServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
        {
            System.out.println("UploadServlet invoked. Here are all uploaded files: ");
            try {
                List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                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());
                    }
                }
            } catch (Exception e) {
                throw new ServletException(e);
            }
        }
    }
    

  4. I registered com.example.UploadServlet in web.xml as follows:

    <servlet>
        <servlet-name>uploadServlet</servlet-name>
        <servlet-class>com.example.UploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>uploadServlet</servlet-name>
        <url-pattern>/uploadServlet</url-pattern>        
    </servlet-mapping>
    

  5. I deployed the project, started the server, went to http://localhost:8080/playground/upload.jsp, selected a random big file from my downloads folder, clicked the Upload link, see the upload percentage counter growing to 100% and I finally see the following in the stdout:

    UploadServlet invoked. Here are all uploaded files: 
    Name: glassfish-v3-windows.exe
    Size: 50402555
    Type: application/octet-stream
    

I'm sorry to say, I can't reproduce your problem. At least, the above information should help you to get started "freshly". Hope it helps.

Update: as per the comments, the filter expects that it is using the same session. Ok, you can do this fairly easy by changing

'<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=<%= decodedString %>',";

to

'<%= request.getContextPath() %>/uploadFile;jsessionid=${pageContext.session.id}?portletId=${portletId}&remoteFolder=<%= decodedString %>',";

这篇关于Uploadify 插件不调用 Java Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆