3

byte[] 形式で画像を受け取る Google アプリ エンジンでサーブレットを実行しています。POST リクエストを使用して、この画像を Google クラウド ストレージに保存したいと考えています。

ここでアイデアを得るには、機能していないが正しい方向にあるコードがあります。

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);
}

org.apache.http.impl.client.DefaultHttpClient; は推奨されていないため、明らかに正しい方法ではありません。インターネットで見つけたものの、実行できなかったもの: http://knowledge-serve.blogspot.com/2012/09/upload-file-on-google-cloud-storage.html ファイルのアップロード - java.net.URLConnection を使用して HTTP 要求を起動および処理する

Google はいくつかの非常に優れたチュートリアルを作成しましたが、html ページから画像をアップロードするためのものです: https://developers.google.com/cloud/samples/photofeed/servephotos

目標は、Google に multipart/data を送信する POST リクエストを作成し、Google がリクエストを GAE サーブレット (この場合は "/upload") に返すことです。したがって、これと同じことを行う必要がありますが、サーブレットと byte[] イメージから:

<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>
4

2 に答える 2

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. 
于 2013-12-16T16:34:26.550 に答える
0

spring MVC サーブレットを使用してファイルをアップロードしています。png ファイルの例を次に示します。

@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]);
} 

次に、URL /gcs/{your_bucket}/{your_file} に PUT リクエスト (POST ではなく) を送信するだけです。

于 2013-10-15T16:30:35.760 に答える