3

I have running servlet on google app engine that receives image in byte[] format. I want to save this image on google cloud storage using POST request.

To get the idea here is some code that is not working but is in the right direction:

public void postReq( byte[] image) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(getUploadUrl());
    FileBody uploadFilePart = new FileBody(?????, "image/jpg");
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("upload-file", uploadFilePart);
    httpPost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httpPost);
}

public String getUploadUrl() {
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    UploadOptions uploadOptions = null;
    String bucket = Constants.BUCKET_NAME;
    uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName(bucket);
    return blobstoreService.createUploadUrl("/upload", uploadOptions);
}

The org.apache.http.impl.client.DefaultHttpClient; is deprecated so obviously is not the right way. Some things that I found in the internet but I couldn't manage to run them: http://knowledge-serve.blogspot.com/2012/09/upload-file-on-google-cloud-storage.html Uploading files - Using java.net.URLConnection to fire and handle HTTP requests

Google made some very good tutorial but is for uploading image from html page: https://developers.google.com/cloud/samples/photofeed/servephotos

The goal is to make POST request sending multipart/data to google and then google returns request to my GAE Servlet in this case to "/upload". So I should do the same as this but in servlet and from byte[] image:

<form action="<%=serviceManager.getUploadUrl()%>" method="post" enctype="multipart/form-data">
    <input id="input-file" class="inactive file btn" type="file" name="photo" onchange="onFileSelected()">
    <textarea name="title" placeholder="Write a description"></textarea>
    <input id="btn-post" class="active btn" type="submit" value="Post">
    <a class="cancel" onclick="togglePhotoPost(false)">Cancel</a>
</form>
Community
  • 1
  • 1
makkasi
  • 2,353
  • 2
  • 35
  • 47

2 Answers2

2
 1. Send the image via json from android device to the server. 
 2. register your two servlets. One for seving the image to Blob store and one for downloading the image:


<servlet>
    <servlet-name>UploadHandlerServlet</servlet-name>
    <servlet-class>.servlet.UploadHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>UploadHandlerServlet</servlet-name>
    <url-pattern>/upload</url-pattern>
</servlet-mapping>
    <servlet>
    <servlet-name>ImagesDownloadServlet</servlet-name>
    <servlet-class>.servlet.ImagesDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ImagesDownloadServlet</servlet-name>
    <url-pattern>/download</url-pattern>
</servlet-mapping>

and one more servlet that receives the post request with the image. Lets call it "PhoneServlet"
 3. From PhoneServlet we are making post request this way:



    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.StringReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    private void sendMultipartDataPostReq(byte[] imageByteArray, long boundary, String type) throws MalformedURLException, IOException {        
        log.warning("sendMultipartDataPostReq( ) START");
        String charset = "UTF-8";
        String CRLF = "\r\n"; // Line separator required by multipart/form-data.
        HttpURLConnection  connection = (HttpURLConnection)new URL(getUploadUrl()).openConnection();
        connection.setInstanceFollowRedirects(true);
        connection.setDoOutput(true);// fires POST request
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        PrintWriter writer = null;
        try {
            OutputStream output = connection.getOutputStream();
            writer = new PrintWriter(new OutputStreamWriter(output, charset), true);// true = autoFlush, important! 
            // Send normal param issueId
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\""+Constants.PARAMETER_TYPE_ISSUEID+"\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
            writer.append(CRLF);
            writer.append(Integer.toString(image.getIssueId())).append(CRLF).flush();
            // Send normal param boundary
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\""+Constants.PARAMETER_TYPE_BOUNDARY+"\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
            writer.append(CRLF);
            writer.append(Long.toString(boundary)).append(CRLF).flush();
            // Send normal param type
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\""+Constants.PARAMETER_TYPE_TYPE+"\"").append(CRLF);
            writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
            writer.append(CRLF);
            writer.append(type).append(CRLF).flush();
            // Send binary file.
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\""+Constants.PARAMETER_TYPE_PHOTO+"\"; filename=\""+ boundary+".jpg" + "\"").append(CRLF);
            writer.append("Content-Type: image/jpg").append(CRLF);
            writer.append("Content-Transfer-Encoding: binary").append(CRLF);
            writer.append(CRLF).flush();
            output.write(imageByteArray);
            output.flush();// Important! Output cannot be closed. Close of writer will close output as well. 
            writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.
            writer.append("--" + boundary + "--").append(CRLF);// End of multipart/form-data.
            writer.close();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                log.warning("COnection HTTP_OK");
            } else {
                log.warning("Server returned HTTP error code");
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, e.toString(), e);
        }
        log.warning("sendMultipartDataPostReq( ) END");
    }

 4. This invokes UploadServlet 

        public class UploadHandlerServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private static Logger log = Logger.getLogger(UploadHandlerServlet.class.getName());

        @Override
        public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
            log.warning("UploadHandlerServlet doPost START");
            BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
            Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
            List<BlobKey> keys = blobs.get(Constants.PARAMETER_TYPE_PHOTO);
            String issueId = req.getParameter(Constants.PARAMETER_TYPE_ISSUEID);
            String type = req.getParameter(Constants.PARAMETER_TYPE_TYPE);
            String boundary = req.getParameter(Constants.PARAMETER_TYPE_BOUNDARY);

            EntityManager em = EMF.get().createEntityManager();
            if (type!=null && type.equals(Constants.IMAGE_TYPE_IMAGE)) {
                log.warning("UploadHandlerServlet doPost: IMAGE_TYPE_IMAGE");
                Issue issue = em.find(Issue.class, Integer.parseInt(issueId));
                //we expect only one image so we get only the first key
                Image image = new Image(issue,keys.get(0).getKeyString(),true, Long.parseLong(boundary));
                em.getTransaction().begin();
                em.persist(image);
                em.getTransaction().commit();
                log.warning("UploadHandlerServlet doPost new Image key is saved");
            }
            else if (type!=null && type.equals(Constants.IMAGE_TYPE_THUMBNAIL)) {
                log.warning("UploadHandlerServlet doPost: IMAGE_TYPE_THUMBNAIL");
                Image image = ImageDAO.getImageByTimeCreated(Long.parseLong(boundary));
                if (image !=null) {
                    image.setBlobKeyThumbnail(keys.get(0).getKeyString());
                    em.getTransaction().begin();
                    em.merge(image);
                    em.getTransaction().commit();
                }
                log.warning("UploadHandlerServlet doPost new Thumbnail key is saved");
            }
            log.warning("UploadHandlerServlet doPost END");
        }
    }

and after UploadHandlerServlet our pictures are in the database as blob keys and in the google BlobStore. 
5. We can download them using the ImagesDownloadServlet:

        public class ImagesDownloadServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        @Override
        public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
            String blobkeyString = req.getParameter("blobkey");
            if (blobkeyString != null) {
                BlobKey blobKey = new BlobKey(blobkeyString);
                BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
                blobstoreService.serve(blobKey, res);
            }
            else {
                res.sendError(400, "One or more parameters are not set");
            }
        }
    }

6. The important thing for making the POST request is to use java.net library and nothing else from apache for example to make POST request. All these servlets are on our server. 
makkasi
  • 2,353
  • 2
  • 35
  • 47
0

I am using spring MVC servlet to upload files. Here is an example for png files:

@RequestMapping(value="/gcs/{bucket}/{id}", method = RequestMethod.PUT)
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws    IOException {
  GcsService gcsService =
               GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
//  GcsFileOptions go = new GcsFileOptions(Builder.);
  GcsFileOptions options = new    GcsFileOptions.Builder().mimeType("image/png").build();
GcsOutputChannel outputChannel =
    gcsService.createOrReplace(getFileName(req), options);
//outputChannel.write(ByteBuffer.wrap(content));
copy(req.getInputStream(), Channels.newOutputStream(outputChannel));
}

private GcsFilename getFileName(HttpServletRequest req) {
String[] splits = req.getRequestURI().split("/", 4);
if (!splits[0].equals("") || !splits[1].equals("gcs")) {
  throw new IllegalArgumentException("The URL is not formed as expected. " +
      "Expecting /gcs/<bucket>/<object>");
}
return new GcsFilename(splits[2], splits[3]);
} 

Then you can just send a PUT request (and not POST) to the url /gcs/{your_bucket}/{your_file}

Dimitrios Douras
  • 508
  • 4
  • 16