ClassNotFoundException App Engine Blobstore教程

时间:2013-09-18 18:36:47

标签: java html google-app-engine servlets

我正在关注本教程:     https://developers.google.com/appengine/docs/java/blobstore/#Uploading_a_Blob

我一直在接受     错误:服务器错误

服务器遇到错误,无法完成您的请求。

当我查看应用引擎控制台时,我不断获得classnotfoundexceptions。我将Upload.java和Serve.java添加到src / com ...文件夹中。但是当我去网站时:

http://instantedituploader.appspot.com

我收到服务器错误500以及以下内容。为什么不能找到这些课程呢?我不熟悉网络方面。是否需要更新某种清单。我已将servlet和servlet映射添加到web.xml。

 2013-09-18 11:27:01.426 /_ah/spi/BackendService.getApiConfigs 500 4501ms 0kb
W 2013-09-18 11:27:01.316 EXCEPTION java.lang.ClassNotFoundException: Serve at com.google.appengine.runtime.Request.process-e9e37a4041165147(Request.java) at java.lang.Class
E 2013-09-18 11:27:01.318 javax.servlet.ServletContext log: unavailable javax.servlet.UnavailableException: Serve at org.mortbay.jetty.servlet.Holder.doStart(Holder.java:79)
W 2013-09-18 11:27:01.321 EXCEPTION java.lang.ClassNotFoundException: Upload at com.google.appengine.runtime.Request.process-e9e37a4041165147(Request.java) at java.lang.Clas
E 2013-09-18 11:27:01.321 javax.servlet.ServletContext log: unavailable javax.servlet.UnavailableException: Upload at org.mortbay.jetty.servlet.Holder.doStart(Holder.java:79)
W 2013-09-18 11:27:01.418 Failed startup of context com.google.apphosting.utils.jetty.RuntimeAppEngineWebAppContext@1f6174a{/,/base/data/home/apps/s~instantedituploader/2.37031
C 2013-09-18 11:27:01.423 Uncaught exception from servlet javax.servlet.UnavailableException: Initialization failed. at com.google.apphosting.runtime.jetty.AppVersionHandlerMa
I 2013-09-18 11:27:01.426 This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This requ

更新。这是web.xml,appengine-web.xml,index.jsp以及upload&从我的项目中提供java文件:

的web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

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

<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value>com.direction.investor.fileuploader.DeviceInfoEndpoint,com.direction.investor.fileuploader.MessageEndpoint</param-value>
</init-param>
</servlet>

<servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>Upload</servlet-class>
</servlet>

<servlet>
<servlet-name>Serve</servlet-name>
<servlet-class>Serve</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Serve</servlet-name>
<url-pattern>/serve</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
</web-app>

应用服务引擎-web.xml中:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>instantedituploader</application>
<version>2</version>

<threadsafe>true</threadsafe>

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>

<sessions-enabled>true</sessions-enabled>

</appengine-web-app>

的index.jsp:

<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>

<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();%>


<html>
<head>
    <title>Upload Test</title>
</head>
<body>
    <form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
        <input type="text" name="foo">
        <input type="file" name="myFile">
        <input type="submit" value="Submit">
    </form>
</body>
</html>

upload.java:

package com.direction.investor.fileuploader;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

public class Upload extends HttpServlet {
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {


    Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
    BlobKey blobKey = blobs.get("myFile");

    if (blobKey == null) {
        res.sendRedirect("/");
    } else {
        res.sendRedirect("/serve?blob-key=" + blobKey.getKeyString());
    }
}
}

serve.java:

package com.direction.investor.fileuploader;


import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

public class Serve extends HttpServlet {
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
        BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
        blobstoreService.serve(blobKey, res);
    }
}

2 个答案:

答案 0 :(得分:1)

在Java中,您需要将类的包与该类所在的路径相匹配。因此,如果您将类放在/com/myname(在/ src下),那么您需要声明package com.myname;作为你班上的第一行。

注意,Eclipse通常使用/ src作为类层次结构的根。如果您想更改,请参阅this answer

答案 1 :(得分:1)

我认为你的web.xml有2个小错误。您应该替换以下两行:

<servlet-class>Upload</servlet-class>
<servlet-class>Serve</servlet-class>

使用:

<servlet-class>com.direction.investor.fileuploader.Upload</servlet-class> <servlet-class>com.direction.investor.fileuploader.Serve</servlet-class>