Read file java from url

Java download file

Often it is required to download a file directly from a URL and save to a directory on local system such as downloading a file from a remote repository. This requires reading a file from url and writing it to a local file.
This article will share different methods to download a file from URL in java. Methods defined here are applicable to all types of file such as a pdf file, an exe file, a txt file or a zip file etc.

  1. Create a connection to the given file url.
  2. Get input stream from the connection. This stream can be used to read file contents.
  3. Create an output stream to the file to be downloaded.
  4. Read the contents from the input stream and write to the output stream.

Java code to download file from URL with this method is given below.

import java.net.URL; import java.net.URLConnection; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileDownloader < public static void main(String[] args) < OutputStream os = null; InputStream is = null; String fileUrl = "http://200.34.21.23:8080/app/file.txt"; String outputPath = "E:\\downloads\\downloaded.txt"; try < // create a url object URL url = new URL(fileUrl); // connection to the file URLConnection connection = url.openConnection(); // get input stream to the file is = connection.getInputStream(); // get output stream to download file os = new FileOutputStream(outputPath); final byte[] b = new byte[2048]; int length; // read from input stream and write to output stream while ((length = is.read(b)) != -1) < os.write(b, 0, length); >> catch (IOException e) < e.printStackTrace(); >finally < // close streams if (os != null) os.close(); if (is != null) is.close(); >> >

Remember that the output and input paths should end with the file name else there will be an error.

Читайте также:  Html css link website

All the methods in this post read a file at a remote URL. If you want to read a file at local system, then refer this post.

  1. Create an input stream to the file to be downloaded.
  2. Create a new channel that will read data from this input stream.
  3. Create an output stream that will write file contents after reading it from the channel created in Step 2.
  4. Get the channel from this output stream and write the contents from channel created in Step 2.

You will understand the algorithm better after looking at the below code example.

import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.io.File; public class FileDownloader < public static void main(String[] args) < try < String fileUrl = "http://200.34.21.23:8080/app/file.pdf"; String outputPath = "E:\\downloads\\downloaded.pdf"; URL url = new URL(fileUrl); // create an input stream to the file InputStream inputStream = url.openStream(); // create a channel with this input stream ReadableByteChannel channel = Channels.newChannel( url.openStream()); // create an output stream FileOutputStream fos = new FileOutputStream( new File(outputPath)); // get output channel, read from input channel and write to it fos.getChannel().transferFrom(channel, 0, Long.MAX_VALUE); // close resources fos.close(); channel.close(); >catch(IOException e) < e.printStackTrace(); >> >

Note that transferFrom() method of a channel takes 3 arguments.
1. input file channel,
2. position at which it will start reading the file, 0 here means the beginning of file, and
3. number of bytes that will be transferred at one time. This value is set to a very large value( Long.MAX_VALUE ) for higher efficiency.

Learn different methods of writing to a file here .

Method 3 : Using Apache Commons IO Library
Apache Commons IO Library has a org.apache.commons.io.FileUtils class which contains a method copyURLToFile() .

Читайте также:  Php string to object name

This method takes two arguments:
1. java.net.URL object pointing to the source file, and
2. java.io.File object which points to an output file path.

Remember that both paths should contain the name of file at the end and output path should be a location on local system at which the file will be downloaded.
copyURLToFile() reads the file from remote location and copies it to the local machine. Example,

import org.apache.commons.io.FileUtils; public class FileDownloader < public static void main(String[] args) < String fileUrl = "http://200.34.21.23:8080/app/file.zip"; String outputPath = "E:\\downloads\\downloaded.zip"; FileUtils.copyURLToFile(new URL(fileUrl), new File(outputPath)); >>

You can add Apache Commons IO dependency to your project as per the build tool.

// Gradle
compile group: ‘org.apache.commons’, name: ‘commons-io’, version: ‘1.3.2’

Hope the article was useful in explaining different ways to download a file from URL in java.
Do not forget to click the clap below.

Источник

Java IO — Reading Content from URL

In this article, we are going to present ways to read content directly from URL in Java. We will use classes available in plain Java like BufferedReader , Scanner , InputStream , and external libraries such as Guava or Apache Commons IO .

This article is a part of Java I/O Series.

2. Reading Directly from a URL using BufferedReader

Let’s start with a simple solution in plain Java. In this example we make the use of InputStreamReader that is a bridge from byte streams to character streams. We are using this class to convert InputStream available under URL to a character-based stream. For better performance, we wrapped InputStreamReader with BufferedReader that uses buffering for efficient reading of characters, arrays, and lines.

package com.frontbackend.java.io.url; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class ReadURLUsingBufferedReader < public static void main(String[] args) throws IOException < String line; StringBuffer buff = new StringBuffer(); URL url = new URL("http://www.example.com/"); try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) < while ((line = in.readLine()) != null) < buff.append(line) .append(System.lineSeparator()); >> System.out.println(buff.toString()); > > 

In this example, we are reading line by line from the URL and append these Strings into StringBuffer using platform-dependent line separator — System.lineSeparator() .

3. Reading content from URL with Scanner

In the next example, we used Scanner class that can parse primitive types and strings using regular expressions.

package com.frontbackend.java.io.url; import java.io.IOException; import java.net.URL; import java.util.Scanner; public class ReadURLUsingScanner < public static void main(String[] args) throws IOException < URL url = new URL("http://www.example.com/"); String content; try (Scanner scanner = new Scanner(url.openStream(), "UTF-8")) < content = scanner.useDelimiter("\\A") .next(); >System.out.println(content); > > 

In this example, we used Scanner with \\A delimiter that matches the beginning of the string. Then invoking next() method returns all characters from beginning to the end of the stream.

4. Get URL content using Java 9 InputStream

In Java 9 there is a nice method that reads all bytes from bytes streams. We can make use it in the following example:

package com.frontbackend.java.io.url; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.charset.StandardCharsets; public class ReadURLUsingInputStream < public static void main(String[] args) throws IOException < URL url = new URL("http://www.google.com/"); try (InputStream inputStream = url.openStream()) < byte[] bytes = inputStream.readAllBytes(); System.out.println(new String(bytes, StandardCharsets.UTF_8)); >> > 

Note that encoding should be always provided on conversions from bytes to characters.

5. Read URL using Guava

Guava library provides Resources.toString(. ) method that allows us to read all content from URL into a String .

package com.frontbackend.java.io.url; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; import com.google.common.io.Resources; public class ReadURLUsingGuava < public static void main(String[] args) throws IOException < URL url = new URL("http://www.google.com/"); String str = Resources.toString(url, StandardCharsets.UTF_8); System.out.println(str); >> 

6. Reading URL content using Apache Commons IO library

Apache Commons IO library comes with IOUtils class that can be used to convert InputStream from URL to String .

package com.frontbackend.java.io.url; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; public class ReadURLUsingApacheCommonsIO < public static void main(String[] args) throws IOException < URL url = new URL("http://www.google.com/"); try (InputStream in = url.openStream()) < String str = IOUtils.toString(in, StandardCharsets.UTF_8); System.out.println(str); >> > 

7. Conclusion

In this article, we presented several ways to read content from the URL in Java. We used classes available in plain Java and libraries such as Guava and Apache Commons IO . Luckily URL object contains method openStream() that returns InputStream . Reading URL actually comes to converting InputStream to a String .

Examples used in this tutorial are available under our GitHub repository.

Источник

Java Download File from URL

Java Download File from URL

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Today we will learn how to download a file from URL in java. We can use java.net.URL openStream() method to download file from URL in java program. We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file.

Java Download File from URL

java download file from url, java code to download file from URL example

Here is the simple java download file from URL example program. It shows both ways to download file from URL in java. JavaDownloadFileFromURL.java

package com.journaldev.files; import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; public class JavaDownloadFileFromURL < public static void main(String[] args) < String url = "https://www.journaldev.com/sitemap.xml"; try < downloadUsingNIO(url, "/Users/pankaj/sitemap.xml"); downloadUsingStream(url, "/Users/pankaj/sitemap_stream.xml"); >catch (IOException e) < e.printStackTrace(); >> private static void downloadUsingStream(String urlStr, String file) throws IOException < URL url = new URL(urlStr); BufferedInputStream bis = new BufferedInputStream(url.openStream()); FileOutputStream fis = new FileOutputStream(file); byte[] buffer = new byte[1024]; int count=0; while((count = bis.read(buffer,0,1024)) != -1) < fis.write(buffer, 0, count); >fis.close(); bis.close(); > private static void downloadUsingNIO(String urlStr, String file) throws IOException < URL url = new URL(urlStr); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(file); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); rbc.close(); >> 

downloadUsingStream: In this method of java download file from URL, we are using URL openStream method to create the input stream. Then we are using a file output stream to read data from the input stream and write to the file. downloadUsingNIO: In this download file from URL method, we are creating byte channel from URL stream data. Then use the file output stream to write it to file. You can use any of these methods to download the file from URL in java program. If you are looking for performance, then do some analysis by using both methods and see what suits your need. You can checkout more Java IO examples from our GitHub Repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us

Источник

Оцените статью