0

有人可以检查下面的代码,看看是否有更好的做法。我真正担心的是性能,我有这个吗?如果没有,我可以做些什么来提高代码的可读性?

try {

    URL url = new URL("http://www.pudim.com.br/SiteBuilder/UploadUsers/pudim.com.br/pudim.jpg");
    URLConnection uc = url.openConnection();
    String type = uc.getContentType();
    BufferedImage image = ImageIO.read(url);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(image, "jpg", os);
    byte[] bytes = os.toByteArray();
    InputStream is = new ByteArrayInputStream(bytes);

    // not important :) ...
} catch (AmazonServiceException e) {
    e.printStackTrace();
} catch (AmazonClientException e) {
    e.printStackTrace();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

欢迎任何考虑。

谢谢!

4

2 回答 2

0

从 URL 读取时,您将遇到所有正常的性能问题 - 连接性/带宽/可用性差;相同的解决方案也适用 - 创建和使用本地缓存,在后台线程上执行加载/大量 IO(以避免阻塞主 UI 线程)。您还需要将代码包装在独立的参数化函数中......

在这个例子中,你实际上并没有对你的结果做任何事情InputStream——也许考虑一下它们是否对于总体上获得你想要的东西是必要的——因为,你每次都将整个图像加载到内存中——如果你全部都不是完全必要的正在做的是将其保存到磁盘。

于 2012-12-11T00:45:45.577 回答
0

如果你想要的只是下载图像,我会使用BufferedInputStream 它来让它尽可能快地从下面的网络读取数据。

他们可以类似地使用 aBufferedOutputStream将其转储到FileOutputStream本地文件的 a 中。

无需进行任何图像处理。

于 2012-12-11T00:48:27.113 回答