I am trying to upload photos to blobstore. The photo gets uploaded succesfully to blobstore but I cannot get the blobkey and servingUrl back. I tried debugging and saw that callback is not made to BlobUpload.java. It gives an error 'Connection to http:// localhost:8888 refused' I am confused because BlobUrlGet.java invokes with no problem and the photo is uploaded. I tried every type of url for the server and the servlets, none of them worked.
EDIT: I realised that if I manually change the 'localhost:8888' to '10.0.2.2:8888' in the debug mode (str variable), 'connection refused' problem goes away but this time it shows below error.
http:// localhost:8888/_ah/upload/ahFteWZpcnN0YXBwZGVtbzEyM3IiCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGICAgICAgN4KDAError 405 HTTP method POST is not supported by this URL
This is the return from post method. It is the same error when I deploy to appengine and work from there.
My code is as below.
BlobUrlGet.java
public class BlobUrlGet extends HttpServlet{
BlobstoreService blServ = BlobstoreServiceFactory.getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String blobUploadUrl = blServ.createUploadUrl("/blob");
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
out.print(blobUploadUrl);
}
}
BlobUpload.java
public class BlobUpload extends HttpServlet {
BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
BlobKey blobKey = blobs.get(0);
ImagesService imagesService = ImagesServiceFactory
.getImagesService();
ServingUrlOptions servingOptions = ServingUrlOptions.Builder
.withBlobKey(blobKey);
String servingUrl = imagesService.getServingUrl(servingOptions);
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/json");
JSONObject json = new JSONObject();
json.put("servingUrl", servingUrl);
json.put("blobKey", blobKey.getKeyString());
PrintWriter out = resp.getWriter();
out.print(json.toString());
out.flush();
out.close();
} catch (JSONException e) {
e.printStackTrace();
}
}
Android client
private class GetBlobUrlTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0){
HttpClient httpClient = new DefaultHttpClient();
//This will invoke "ImgUpload servlet
HttpGet httpGet = new HttpGet("http://10.0.2.2:8888/bloburl");
HttpResponse response;
try {
response = httpClient.execute(httpGet);
HttpEntity urlEntity = response.getEntity();
InputStream in = urlEntity.getContent();
String str = "";
StringWriter writer = new StringWriter();
String encoding = "UTF-8";
IOUtils.copy(in, writer, encoding);
str = writer.toString();
// str = URLDecoder.decode(str, "UTF-8");
HttpPost httppost = new HttpPost(str);
File f = new File(picturePath);
FileBody fileBody = new FileBody(f);
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.addPart("file", fileBody);
reqEntity.addBinaryBody("Photo", f, ContentType.create("image/jpeg"), "foto2.jpg");
httppost.setEntity(reqEntity.build());
response = httpClient.execute(httppost); //Here "uploaded" servlet is automatically invoked
urlEntity = response.getEntity(); //Response will be returned by "uploaded" servlet in JSON format
in = urlEntity.getContent();
str = "";
IOUtils.copy(in, writer, encoding);
str = writer.toString();
JSONObject resultJson = new JSONObject(str);
blobKey = resultJson.getString("blobKey");
servingUrl = resultJson.getString("servingUrl");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
web.xml
<servlet>
<servlet-name>BlobstoreURL</servlet-name>
<servlet-class>com.google.samplesolutions.mobileassistant.BlobUrlGet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BlobstoreURL</servlet-name>
<url-pattern>/bloburl</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BlobstoreUpload</servlet-name>
<servlet-class>com.google.samplesolutions.mobileassistant.BlobUpload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BlobstoreUpload</servlet-name>
<url-pattern>/blob</url-pattern>
</servlet-mapping>
Thanks in advance.
doPost should have been overridden. Now it works perfectly.
public class BlobUpload extends HttpServlet {
BlobstoreService blobstoreService = BlobstoreServiceFactory
.getBlobstoreService();
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
BlobKey blobKey = blobs.get(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With