GWT文件上传报错,组件为Apache的FileUpload

xiongying1 发布于 2013/01/06 13:51
阅读 598
收藏 0
GWT

    我照着GWT揭秘的书写的上传报错:

1:文件上传的界面:

package chapter4.regex.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FileLoadTest implements EntryPoint{
    public void onModuleLoad(){
        final FormPanel formPanel = new FormPanel();
        formPanel.setAction(GWT.getHostPageBaseURL() + "fileupload.jsp");
        formPanel.setMethod(FormPanel.METHOD_POST);
        formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
        formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler(){
            @Override
            public void onSubmitComplete(SubmitCompleteEvent event){
                Window.alert(event.getResults());
            }
        });
        
        VerticalPanel verticalPanel = new VerticalPanel();
        FileUpload fileUpload = new FileUpload();
        fileUpload.setName("file");
        verticalPanel.add(fileUpload);
        Button button = new Button("upload");
        button.addClickHandler(new ClickHandler(){
            @Override
            public void onClick(ClickEvent event){
                formPanel.submit();
            }
        });
        verticalPanel.add(button);
        verticalPanel.add(formPanel);
    }

}

war目录下的fileupload.jsp文件:

<%@ page language="java" contentType="text/html; charset=GB2312"
    pageEncoding="GB2312"%>
<%@ page import="java.io.*,org.apache.commons.fileupload.*,org.apache.commons.fileupload.servlet.*,
org.apache.commons.fileupload.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>Insert title here</title>
</head>
<body>
  <%
   ServletFileUpload upload = new ServletFileUpload();
   String fileName = null;
   FileItemIterator iter;
   iter = upload.getItemIterator(request);
   while(iter.hasNext()){
       FileItemStream item = iter.next();
       InputStream stream = item.openStream();
       
       if(!item.isFormField()){
           if(item.getFieldName().equals("file")){
               fileName = Streams.asString(stream,"utf-8");
               out.print("ok");
           }
       }
   }
  %>
</body>
 
</html>

运行时报错:

[WARN] /fileupload.jsp
org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:908)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148)
    at org.apache.jsp.fileupload_jsp._jspService(fileupload_jsp.java:60)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
[ERROR] 500 - GET /fileupload.jsp?gwt.codesvr=127.0.0.1:9997 (127.0.0.1) 3908 bytes

加载中
返回顶部
顶部