1
我正在嘗試使用Blobstore創建一個簡單的Web應用程序來上傳多個圖像。我設法成功地將圖像上傳到Blobstore。但是當圖像上傳時,上傳的servlet不會重定向到任何我嘗試重定向到的頁面。它只是顯示一個空白頁面,其URL如下所示:myapplicationId.appstore.com/_ah/upload/....(keyvalue)Google App Engine Blobstore圖像
我無法更改名爲Upload的servlet重定向到另一個頁面。我也試圖服務的圖像,但無法做到這一點。
有人可以指導我。
我正在使用Java中的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");
}
}
我要去哪裏錯了?