如何从URL对象创建文件对象

时间:2021-01-05 02:48:04

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对象创建一个文件对象我的要求是我需要创建一个web图像的文件对象(比如googles徽标)

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

6 个解决方案

#1


80  

Use Apache Common IO's FileUtils:

使用Apache Common IO的FileUtils:

import org.apache.commons.io.FileUtils

FileUtils.copyURLToFile(url, f);

The method downloads the content of url and saves it to f.

该方法下载url的内容并保存到f。

#2


18  

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:

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

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.

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

#3


12  

In order to create a File from a HTTP URL you need to download the contents from that URL:

为了从HTTP URL创建一个文件,您需要从该URL下载内容:

URL url = new URL("http://www.google.ro/logos/2011/twain11-hp-bg.jpg");
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("downloaded.jpg"));
byte[] buf = new byte[512];
while (true) {
    int len = in.read(buf);
    if (len == -1) {
        break;
    }
    fos.write(buf, 0, len);
}
in.close();
fos.flush();
fos.close();

The downloaded file will be found at the root of your project: {project}/downloaded.jpg

下载的文件将在项目的根目录:{项目}/下载。jpg中找到

#4


10  

URL url = new URL("http://google.com/pathtoaimage.jpg");
File f = new File(url.getFile());

#5


8  

Since Java 7

因为Java 7

File file = Paths.get(url.toURI()).toFile();

#6


1  

import java.net.*; 
import java.io.*; 
class getsize { 
    public static void main(String args[]) throws Exception {
        URL url=new URL("http://www.supportyourpm.in/jatin.txt"); //Reading
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();

        //Getting size
        HttpURLConnection conn = null;
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("HEAD");
        conn.getInputStream();
        System.out.println("Length : "+conn.getContentLength());
    } 
}

#1


80  

Use Apache Common IO's FileUtils:

使用Apache Common IO的FileUtils:

import org.apache.commons.io.FileUtils

FileUtils.copyURLToFile(url, f);

The method downloads the content of url and saves it to f.

该方法下载url的内容并保存到f。

#2


18  

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:

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

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.

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

#3


12  

In order to create a File from a HTTP URL you need to download the contents from that URL:

为了从HTTP URL创建一个文件,您需要从该URL下载内容:

URL url = new URL("http://www.google.ro/logos/2011/twain11-hp-bg.jpg");
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("downloaded.jpg"));
byte[] buf = new byte[512];
while (true) {
    int len = in.read(buf);
    if (len == -1) {
        break;
    }
    fos.write(buf, 0, len);
}
in.close();
fos.flush();
fos.close();

The downloaded file will be found at the root of your project: {project}/downloaded.jpg

下载的文件将在项目的根目录:{项目}/下载。jpg中找到

#4


10  

URL url = new URL("http://google.com/pathtoaimage.jpg");
File f = new File(url.getFile());

#5


8  

Since Java 7

因为Java 7

File file = Paths.get(url.toURI()).toFile();

#6


1  

import java.net.*; 
import java.io.*; 
class getsize { 
    public static void main(String args[]) throws Exception {
        URL url=new URL("http://www.supportyourpm.in/jatin.txt"); //Reading
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();

        //Getting size
        HttpURLConnection conn = null;
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("HEAD");
        conn.getInputStream();
        System.out.println("Length : "+conn.getContentLength());
    } 
}