使用multipart - Java,GWT时无法提取表单字段数据

时间:2014-11-12 07:13:42

标签: java gwt multipart

onModuleLoad()代码:

           VerticalPanel vPanel = new VerticalPanel();

           final FormPanel form= new FormPanel();
           form.setAction("/bq_stream/upload");
           form.setEncoding(FormPanel.ENCODING_MULTIPART);
           form.setMethod(FormPanel.METHOD_POST);

           final TextBox txt_name = new TextBox();
           txt_pid.setName("Name");

           Button uploadButton = new Button("Upload");
           uploadButton.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent event) {
                    form.submit();
                }
            });

          form.setWidget(vPanel);

Http Servlet代码:

@Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) 
                throws ServletException, IOException{
        String name= null;
        boolean isMultiPart= true;

        if(isMultiPart){

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory); 

            try {
                @SuppressWarnings("unchecked")
                List<FileItem> items = upload.parseRequest(req);
                Iterator<FileItem> iterator = items.iterator();
                while(iterator.hasNext()){

                    FileItem item = (FileItem) iterator.next();
                    if(item.isFormField()){

                        String fieldname= item.getFieldName();  
                        String fieldvalue = item.getString();

                        if( (fieldname!=null) && (fieldvalue!=null) ){
                            if(fieldname.equals("name")){
                                projectID= fieldvalue.toString();
                                System.out.println(name);
                            }
                        }   
                    }
                }
            } catch (FileUploadException e) {
                    e.printStackTrace();
            }
        }            

从Eclipse(Kepler)作为Web应用程序运行项目时,无法提取和打印文本框数据并在控制台中出现跟随错误

Nov 12, 2014 7:00:07 AM com.google.appengine.tools.development.DevAppServerModulesFilter doDirectRequest
FINEST: request to specific module instance: -1.default
Nov 12, 2014 7:00:07 AM com.google.appengine.tools.development.DevAppServerModulesFilter doDirectRequest
FINEST: Acquired serving permit for: -1.default
Nov 12, 2014 7:00:07 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Error for /bq_stream/upload
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:428)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:127)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:491)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileUploadException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:216)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 43 more

Nov 12, 2014 7:00:07 AM com.google.apphosting.utils.jetty.AppEngineAuthentication$AppEngineAuthenticator authenticate
FINE: Got /bq_stream/upload with null response, returning null.

提前致谢。

2 个答案:

答案 0 :(得分:2)

您没有在/WEB-INF/lib文件夹中包含公共jar。将它添加到类路径中是不够的。

在Eclipse中,选择您的项目,然后选择“问题”选项卡。您可能会在那里看到警告说服务器端将无法使用资源。右键单击此警告,然后选择“复制...”选项。否则,请手动将此jar添加到/WEB-INF/lib文件夹。

答案 1 :(得分:1)

您似乎错过了类路径中的commons-fileupload.jar。下载并将其添加到类路径中。