首页 文章

GWT FileUpload问题:Content-Type为'multipart/form-data; Expected ' text / x-gwt-rpc'

提问于
浏览
0

我有一些麻烦上传文件到服务器 . 我使用了这个教程:http://code.google.com/p/gwtupload/wiki/GwtUpload_GettingStarted并且一切顺利,但是当我选择一个文件时,进度条没有显示任何进度,在Eclipse中我得到:

[WARN]调度传入的RPC调用时发生异常javax.servlet.ServletException:Content-Type为'multipart / form-data;边界= ---- webkitformboundaryfafzb7tzbpq9rkjl” . 预计'text / x-gwt-rpc' . 在com.google.gwt.user.server.rpc.RPCServletUtils.checkContentTypeIgnoreCase(RPCServletUtils.java:476)....

我开始在GWT的HelloWorld初始项目之上添加教程中的代码 .

这是我的web.xml文件

<context-param>
    <!-- max size of the upload request -->
    <param-name>maxSize</param-name>
    <param-value>3145728</param-value>
</context-param>
<context-param>
    <!-- Useful in development mode to slow down the uploads in fast networks. 
        Put the number of milliseconds to sleep in each block received in the server. 
        false or 0, means don't use slow uploads -->
    <param-name>slowUploads</param-name>
    <param-value>200</param-value>
</context-param>

<!-- Servlets -->
<servlet>
    <servlet-name>greetServlet</servlet-name>
    <servlet-class>webapp.server.GreetingServiceImpl</servlet-class>

    <servlet-name>uploadServlet</servlet-name>
    <!-- This is the default servlet, it puts files in session -->
    <servlet-class>webapp.server.CustomizedUploadServlet</servlet-class>

</servlet>

<servlet-mapping>
    <servlet-name>greetServlet</servlet-name>
    <url-pattern>/singlefileuploadsample/greet</url-pattern>

    <servlet-name>uploadServlet</servlet-name>
    <url-pattern>*.gupld</url-pattern>

    <servlet-name>uploadServlet</servlet-name>
    <url-pattern>/upload</url-pattern>

</servlet-mapping>

<!-- Default page to serve -->
<welcome-file-list>
    <welcome-file>SingleFileUploadSample.html</welcome-file>
</welcome-file-list>

对于servlet,我创建了一个新类并在其中添加了代码 . 有一些与内容类型相关的内容,但我无法弄清楚如何解决这个问题 .

更新:

当我尝试在Jetty上部署项目时,这只发生在Eclipse中 . 一旦在Tomcat上作为war文件部署,我工作得很好 .

4 回答

  • 0

    您的错误跟踪表明已收到您发送的请求的servlet是gwt-rpc servlet .

    看来您的web.xml配置正确,根据该文件假设您的webapp.server.CustomizedUploadServlet处理了请求,因此我看到的唯一可能的错误原因是:

    • 您的webapp.server.CustomizedUploadServlet正在扩展RPCServlet而不是UploadAction .

    • 或您的部署错误,您的应用程序没有读取正确的web.xml

  • 0

    mP是正确的,您不能将带有文件的表单发布到RPC servlet . 因此,不是扩展RemoteServiceServlet而是创建一个扩展javax.servlet.http.HttpServlet的servlet . 我自己做了这个更改(使用this问题中的代码)并成功上传了一个文件 .

  • 0

    您无法发布包含要上载到RPC服务的文件的表单 . 扩展RPC servlet的服务无法接收表单,但只能使用正确的对象 .

  • 0

    我使用gwtupload进行文件上传

    import gwtupload.server.UploadAction;
    import gwtupload.server.exceptions.UploadActionException;
    
    import java.io.File;
    import java.util.Hashtable;
    import java.util.List;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.fileupload.FileItem;
    
    public class CWTUploadServlet extends UploadAction {
    
      private static final long serialVersionUID = 1L;
    
      Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
      /**
       * Maintain a list with received files and their content types. 
       */
      Hashtable<String, File> receivedFiles = new Hashtable<String, File>();
    
      /**
       * Override executeAction to save the received files in a custom place
       * and delete this items from session.  
       */
      @Override
      public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
        String response = "";
        int cont = 0;
        for (FileItem item : sessionFiles) {
          if (false == item.isFormField()) {
            cont++;
            try {
    
               //Create a new file based on the remote file name in the client
               String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");
               System.out.println("Save name : "+saveName);
               File file =new File("/Users/Spirit/hob/" + saveName);
    
              item.write(file);
              /// Save a list with the received files
              receivedFiles.put(item.getFieldName(), file);
              receivedContentTypes.put(item.getFieldName(), item.getContentType());
    
              /// Compose a xml message with the full file information
              response += "<file-" + cont + "-field>" + item.getFieldName() + "</file-" + cont + "-field>\n";
              response += "<file-" + cont + "-name>" + item.getName() + "</file-" + cont + "-name>\n";
              response += "<file-" + cont + "-size>" + item.getSize() + "</file-" + cont + "-size>\n";
              response += "<file-" + cont + "-type>" + item.getContentType() + "</file-" + cont + "type>\n";
            } catch (Exception e) {
              throw new UploadActionException(e);
            }
          }
        }
    
        /// Remove files from session because we have a copy of them
        removeSessionFileItems(request);
    
        /// Send information of the received files to the client.
        return "<response>\n" + response + "</response>\n";
      }
    

    还有你的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
    
        <display-name>CWTMVP</display-name>
    
        <!-- Default page to serve -->
        <welcome-file-list>
            <welcome-file>CWTMVP.html</welcome-file>
        </welcome-file-list>
    
        <!--
            This Guice listener hijacks all further filters and servlets. Extra
            filters and servlets have to be configured in your
            ServletModule#configureServlets() by calling
            serve(String).with(Class<? extends HttpServlet>) and
            filter(String).through(Class<? extends Filter)
        -->
        <listener>
            <listener-class>com.db.cawt.clientzonedesign.server.guice.GuiceServletConfig</listener-class>
        </listener>
    
        <filter>
            <filter-name>guiceFilter</filter-name>
            <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>guiceFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
          <context-param>
            <param-name>maxSize</param-name>
            <param-value>102400000</param-value>
          </context-param>
    
          <context-param>
            <param-name>slowUploads</param-name>
            <param-value>true</param-value>
          </context-param>
    
          <servlet>
            <servlet-name>uploadServlet</servlet-name>
            <servlet-class>xxx.yyy.server.CWTUploadServlet</servlet-class>
          </servlet>
    
          <servlet-mapping>
            <servlet-name>uploadServlet</servlet-name>
            <url-pattern>*.gupld</url-pattern>
          </servlet-mapping>
    
    
    </web-app>
    

相关问题