我正在开发一个Google App Engine项目。事实上,我想将图像保存在blobstore中, 之后,我恢复了blob密钥和服务URL,并将它们存储在数据存储区中的Demand实体中。
我的jsp文件是:
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.blobstore.*"%>
<%@ page import="com.Upload"%>
<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/style.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="container-narrow">
<div class="masthead">
<ul class="nav nav-pills pull-right">
<li class="active"><a href="/">Accueil</a></li>
<li><a href="#upload">Upload</a></li>
</ul>
<h3 class="muted">Cloud Uploader</h3>
</div>
<hr />
<form
action="<%= blobstoreService.createUploadUrl("/") %>"
method="post" enctype="multipart/form-data">
<p>
<label>Fichier à envoyer : <input type="file" name="uploadedFile" /></label>
</p>
<div class="form-actions">
<input type="submit" class="btn" />
</div>
</form>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
我的servlet处理请求:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
this.getServletContext().getRequestDispatcher("/UploadForm.jsp").forward(req, resp);}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
System.out.println(blobKeys.get(0).toString());//No result
DataManager dat=new DataManager(); // this class contains DemandCreate method
String personPhotoServingUrl= imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKeys.get(0)));
String personPhotoBlobKey=blobKeys.get(0).toString();
dat.DemandCreate("id3", "", "", "", "", 1, "", "", 3031, "", "", personPhotoBlobKey, personPhotoServingUrl);// this method enables user to store data in Datasotre using Objectify..
resp.sendRedirect("/");}
问题是当我上传图像时,它工作正常,blob和图像在BlobInfo实体中找到但在我的Demand实体中没有添加结果(在此实体中没有新插入)。 我试图打印插入图像的blob键:
System.out.println(blobKeys.get(0).toString());//No result
但是,没有结果。 谢谢你的帮助。
答案 0 :(得分:0)
blobs是一张地图。你想要地图blob的密钥还是BlobKeys的列表?
要获取地图的密钥,您可以执行以下操作:
blob.keySet()。指定者()[0]
要获取BlobKeys列表,您可以这样做:
blob.get(blob.keySet()。指定者()[0])。得到(0)的ToString()
答案 1 :(得分:0)
您将blob数据保存到blobs
,而不是blobKeys
。
Imho你应该这样做:
System.out.println(blobs.get("uploadedFile").toString());
答案 2 :(得分:0)
问题解决了,这是因为在jsp文件中有一个值为“/”的成功路径:
<form
action="<%= blobstoreService.createUploadUrl("/") %>"
method="post" enctype="multipart/form-data">
successPath是相对URL,将在用户成功上载blob后调用。 因此,当blob成功上传时,jsp返回到主目录并且不执行doPost()方法的代码。因此,我通过将doPost方法的代码复制到其他命名的servlet来解决了这个问题。例如,使用NewDemandHandler并将successpath编辑为此servlet的相对路径:
<form
action="<%= blobstoreService.createUploadUrl("/NewDemandHandler") %>"
method="post" enctype="multipart/form-data">