如何从URL对象创建文件对象 [英] How to create file object from URL object

查看:105
本文介绍了如何从URL对象创建文件对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从URL对象创建一个File对象
我的要求是
我需要创建一个web图像的文件对象(比如googles logo)

I need to create a File object from URL object My requirement is I need to create a file object of a web image (say googles logo)

URL url = new URL("http://google.com/pathtoaimage.jpg");
File f = create image from url object


推荐答案

您可以使用 ImageIO 以便从URL加载图像,然后将其写入文件。这样的事情:

You can make use of ImageIO in order to load the image from an URL and then write it to a file. Something like this:

URL url = new URL("http://google.com/pathtoaimage.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("downloaded.jpg");
ImageIO.write(img, "jpg", file);

如果需要,您还可以将图像转换为其他格式。

This also allows you to convert the image to some other format if needed.

这篇关于如何从URL对象创建文件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆