I'm developping a Google App Engine project.In fact, i want to save the images in the blobstore, after that,i recover the blob key and the serving url, and i store them in an Demand entity in Datastore.
My jsp file is :
<%@ 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>
My servlet which handles the request:
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("/");}
The problem is when i upload an image,it works fine, the blob and the image is found in the BlobInfo entity but no result added in my Demand entity (no new insertion in this entity). I tried to print the blob key of the image inserted :
System.out.println(blobKeys.get(0).toString());//No result
But, no result. Thanks for your Help.
The problem is solved, it's due to the successpath which had a value "/" in the jsp file:
<form
action="<%= blobstoreService.createUploadUrl("/") %>"
method="post" enctype="multipart/form-data">
The successPath is relative URL which will be invoked after the user successfully uploads a blob. So when blob is succesfully uploaded,the jsp returns to the principal directory and doesn't execute the code of the doPost() method.For that reason, i've solved the issue by copying the code of the doPost method to other servlet named for exemple NewDemandHandler and editing the successpath to the relative path of this servlet:
<form
action="<%= blobstoreService.createUploadUrl("/NewDemandHandler") %>"
method="post" enctype="multipart/form-data">