加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

错误:java.lang.UnsupportedOperationException:使用App Engin

发布时间:2020-12-14 05:47:55 所属栏目:Java 来源:网络整理
导读:我需要使用App Engine BlobStore检索上传图像的高度和宽度.为了找到我使用以下代码: try { Image im = ImagesServiceFactory.makeImageFromBlob(blobKey); if (im.getHeight() == ht im.getWidth() == wd) { flag = true; } } catch (UnsupportedOperationE
我需要使用App Engine BlobStore检索上传图像的高度和宽度.为了找到我使用以下代码:
try {
            Image im = ImagesServiceFactory.makeImageFromBlob(blobKey);

            if (im.getHeight() == ht && im.getWidth() == wd) {
                flag = true;
            }
        } catch (UnsupportedOperationException e) {

        }

我可以上传图像并生成BlobKey但是当将Blobkey传递给makeImageFromBlob()时,它会生成以下错误:

java.lang.UnsupportedOperationException: No image data is available

如何解决这个问题或任何其他方式直接从BlobKey找到图像的高度和宽度.

解决方法

Image上的大多数方法本身都会抛出UnsupportedOperationException.
所以我使用com.google.appengine.api.blobstore.BlobstoreInputStream.BlobstoreInputStream来操作blobKey中的数据.这是我可以获得图像宽度和高度的方式.
byte[] data = getData(blobKey);
Image im = ImagesServiceFactory.makeImage(data);
if (im.getHeight() == ht && im.getWidth() == wd) {}
private byte[] getData(BlobKey blobKey) {
    InputStream input;
    byte[] oldImageData = null;
    try {
        input = new BlobstoreInputStream(blobKey);
                ByteArrayOutputStream bais = new ByteArrayOutputStream();
        byte[] byteChunk = new byte[4096];
        int n;
        while ((n = input.read(byteChunk)) > 0) {
            bais.write(byteChunk,n);
        }
        oldImageData = bais.toByteArray();
    } catch (IOException e) {}

    return oldImageData;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读