如何在谷歌云桶上改善图像上传和下载时间

时间:2015-08-22 18:13:18

标签: android google-app-engine google-cloud-storage google-cloud-endpoints google-cloud-platform

我正在上传&通过在Android-Studio中创建的项目下载在Google Cloud上创建的存储桶中的文件。

我觉得图片上传和下载需要很长时间。

我还通过启用自动缩放来检查它,我的appengine-web.xml文件看起来像:

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>APP_ID</application>
    <version>1</version>
    <threadsafe>true</threadsafe>
    <instance-class>F4_1G</instance-class>

    <automatic-scaling>
        <min-idle-instances>1</min-idle-instances>
        <!-- ‘automatic’ is the default value. -->
        <max-idle-instances>automatic</max-idle-instances>
        <!-- ‘automatic’ is the default value. -->
        <min-pending-latency>30ms</min-pending-latency>
        <max-pending-latency>automatic</max-pending-latency>
        <max-concurrent-requests>50</max-concurrent-requests>
    </automatic-scaling>

    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
        <property name="gcm.api.key" value="gcm_key" />
    </system-properties>
</appengine-web-app>

Servlet to Upload图像如下所示:

public class UploadImageServlet extends HttpServlet {

    private static final Logger logger = Logger.getLogger(UploadImageServlet.class.getName());

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ServletFileUpload upload = new ServletFileUpload();
            resp.setContentType("text/plain");

            FileItemIterator iterator = upload.getItemIterator(req);
            ImageJson data = new ImageJson();
            byte[] image = null;
            byte[] imageThumbnail = null;

            while (iterator.hasNext()) {
                FileItemStream item = iterator.next();
                InputStream stream = item.openStream();

                if (item.isFormField()) {
                    String field = item.getFieldName();
                    String value = IOUtils.toString(stream);
                    try {
                        if (field.equalsIgnoreCase(SESSION_PARAM_IN_FORM)) {
                            data.session = value;
                        } else if (field.equalsIgnoreCase(USER_PARAM_IN_FORM)) {
                            data.user = Long.parseLong(value);
                        }
                    } catch (NumberFormatException e) {
                        logger.warning("Invalid " + field);
                    }
                    // user, session

                } else {
                    logger.info("Got an uploaded file: " + item.getFieldName() + ", name = " + item.getName());

                    data.imageName = item.getName();


                    // You now have the filename (item.getName() and the
                    // contents (which you can read from stream). Here we just
                    // print them back out to the servlet output stream, but you
                    // will probably want to do something more interesting (for
                    // example, wrap them in a Blob and commit them to the
                    // datastore).


                    image = IOUtils.toByteArray(stream);

                    System.out.println("Creating Thumbnail...");
                    ImagesService imagesService = ImagesServiceFactory.getImagesService();
                    Image oldImage = ImagesServiceFactory.makeImage(image);

                    Transform resize = ImagesServiceFactory.makeResize(120, 120);

                    //Resize The Image using the transform created above
                    Image resizedImage = imagesService.applyTransform(resize, oldImage);
                    imageThumbnail = resizedImage.getImageData();

                    System.out.println("Thumbnail created!!");
                }
            }

            resp.getWriter().write(new Gson().toJson(saveImage(data, image, imageThumbnail)));

        } catch (Exception ex) {
            throw new ServletException(ex);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您必须使用Json格式上传和下载图像。 如果您想了解更多信息,请与我联系 希望会有所帮助