Google App Engine Blobstore图片

时间:2013-12-08 05:00:54

标签: java google-app-engine jsp servlets blobstore

我正在尝试使用Blobstore创建一个简单的Web应用程序来上传多个图像。我已成功将图像上传到Blobstore。但是当图像上传时,上传的servlet不会重定向到我尝试将其重定向到的任何页面。它只显示一个空白页面,其URL如:myapplicationId.appstore.com/_ah/upload/....(keyvalue)

我无法将名为Upload的servlet重定向更改为另一个页面。我也试图提供图像,但我无法做到。

有人可以指导我。

我正在使用Google中的标准代码来上传图片。

我的JSP是:

<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
 <script>
<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

%>

</script>
<html>
<head>
<title>Show My Picture(s)</title>
</head>
<body>

<h1>Show My Picture(s)</h1>
<hr/>
<h2>Upload picture(s)</h2>
<form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
Upload File(s) :
<input type="text" name="filename"/>
<input type="file" name="myPics" multiple="multiple"/>
<input type="submit" value="Upload Picture"/>
</form>
</html>

我上传的Servlet:

package photos;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.logging.Logger;

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

import photos.Upload;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * Servlet implementation class Upload
 */
@WebServlet("/Upload")
public class Upload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    private final static Logger _logger = Logger.getLogger(Upload.class.getName());
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

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

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


            if (blobKey == null) {
                res.sendRedirect("/");
            }
            else {
                res.sendRedirect("/upload?blob-key="+blobKey.getKeyString()+"blobSize="+blobs.size());
              }
            }catch(Exception e){
                e.printStackTrace();
                System.out.println("Document failed to POST, redirecting back to upload.");
                res.sendRedirect("/index.jsp");
            }
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        String uploadedImageKey = req.getParameter("blob-key");
        PrintWriter out = resp.getWriter();
        resp.setHeader("Content-Type", "text/html");
        resp.sendRedirect("/viewPics.jsp");
        //out.println(req.getParameter("blobSize")+" images have been successfully uploaded");



    }
}

我哪里错了?

1 个答案:

答案 0 :(得分:0)

我想自助是最好的帮助。我解决了这个问题。它只是在createUploadUrl中将上传更改为Upload。