There is an online file (such as http://www.example.com/information.asp
) I need to grab and save to a directory. I know there are several methods for grabbing and reading online files (URLs) line-by-line, but is there a way to just download and save the file using Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Below is the sample code to download movie from internet with java code:
There is method U.fetch(url) in underscore-java library.
pom.xml:
Code example:
When using
Java 7+
use the following method to download a file from the Internet and save it to some directory:Documentation here.
There are many elegant and efficient answers here. But the conciseness can make us lose some useful information. In particular, one often does not want to consider a connection error an Exception, and one might want to treat differently some kind of network-related errors - for example, to decide if we should retry the download.
Here's a method that does not throw Exceptions for network errors (only for truly exceptional problems, as malformed url or problems writing to the file)
It's possible to download the file with with Apache's
HttpComponents
instead ofCommons-IO
. This code allows you to download a file in Java according to its URL and save it at the specific destination.In contrast to the single line of code:
this code will give you more control over a process and let you specify not only time outs but
User-Agent
andReferer
values, which are critical for many web-sites.