0

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

[WARN] 调度传入 RPC 调用 javax.servlet.ServletException 时出现异常:Content-Type 为 'multipart/form-data; 边界=----webkitformboundaryfafzb7tzbpq9rkjl'。预期为“文本/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

4 回答 4

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>
于 2012-10-30T03:39:41.627 回答
0

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

于 2014-11-02T18:07:15.260 回答
0

您不能发布包含要上传到 RPC 服务的文件的表单。扩展 RPC servlet 的服务无法接收表单,而只能获取适当的对象。

于 2012-10-30T01:04:41.270 回答
0

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

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

  • 您的 webapp.server.CustomizedUploadServlet 正在扩展 RPCServlet 而不是 UploadAction。
  • 或者您的部署错误并且您的应用程序没有读取正确的 web.xml
于 2012-10-30T13:21:35.087 回答