2

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

我不断收到错误:服务器错误

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

当我查看应用引擎控制台时,我不断收到 classnotfoundexceptions。我将 Upload.java 和 Serve.java 添加到 src/com... 文件夹。但是当我访问该站点时:

http://instantedituploader.appspot.com

我收到服务器错误 500 以及以下内容。为什么找不到类?我对web端不是很熟悉。是否有某种我需要更新的清单。我已将 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 以及上传和服务 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>

appengine-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>

上传.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());
    }
}
}

服务.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);
    }
}
4

2 回答 2

1

我认为您的 web.xml 有 2 个小错误。您应该替换以下 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>

于 2013-09-23T17:59:20.537 回答
1

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

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

于 2013-09-19T07:00:44.477 回答