调用HttpServletRequest.getPart(String)

时间:2016-03-04 15:44:29

标签: java maven http google-app-engine servlets

我一直在关注AppEngine的Google留言板教程,并成功完成了教程。然后我继续添加我自己的部分(将图像上传到数据存储区)只是为了挑战,但遇到了问题。我的代码编译和部署很好,但是在向以下表单提交.jpg时,

<form action="/image" method="post" enctype="multipart/form-data">
    <div><input type="text" name="imageName"></input></div>
    <div><input type="file" name="image" accept="image/*"></input></div>
    <div><input type="submit" value="Post Image"/></div>
</form>

我收到以下错误

    java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getPart(Ljava/lang/String;)Ljavax/servlet/http/Part;
        at com.example.guestbook.ImageServlet.doPost(ImageServlet.java:49)
...

我的pom中的servlet依赖关系如下:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

的web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
            http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
      <servlet-name>sign</servlet-name>
      <servlet-class>com.example.guestbook.SignGuestbookServlet</servlet-class>
    </servlet>
    <servlet>
      <servlet-name>image</servlet-name>
      <servlet-class>com.example.guestbook.ImageServlet</servlet-class>
    </servlet>

    <servlet-mapping>
      <servlet-name>sign</servlet-name>
      <url-pattern>/sign</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
      <servlet-name>image</servlet-name>
      <url-pattern>/image</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>guestbook.jsp</welcome-file>
    </welcome-file-list>

    <filter>
      <filter-name>ObjectifyFilter</filter-name>
      <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>ObjectifyFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
      <listener-class>com.example.guestbook.OfyHelper</listener-class>
    </listener>
</web-app>

并且为请求提供服务的servlet代码是:

@MultipartConfig
public class ImageServlet extends HttpServlet {

  // Process the http POST of the form
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();  // Find out who the user is.

    String imageName = req.getParameter("imageName");
    try
    {
        Part part = req.getPart("image");
        InputStream is = part.getInputStream();
        byte[] bytes = null;
        is.read(bytes);
        Blob image = new Blob(bytes);

        Image newImage = new Image(imageName, image);

        // Use Objectify to save the greeting and now() is used to make the call synchronously as we
        // will immediately get a new page using redirect and we want the data to be present.
        ObjectifyService.ofy().save().entity(newImage).now();
    }
    catch(ServletException e)
    {
        e.printStackTrace();
    }

    resp.sendRedirect("/guestbook.jsp");
  }
}

“Image”模型为:

@Entity
public class Image {
  @Id public Long id;

  public String imageName;
  public Blob image;

  /**
   * Simple constructor
   **/
  public Image() {
  }

  /**
   * Takes all important fields
   **/
  public Image(String name, Blob newImage) {
    imageName = name;
    image = newImage;
  }

}

我最初遇到的问题是教程pom提供的servlet版本不支持“getPart()”(2.7我相信),但我将其更新为3.1并且仍然受到NoSuchMethodError的欢迎。我想有可能仍然有一些人工制品仍在使用过时的servlet版本,但我个人觉得很难过。

0 个答案:

没有答案