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
You'll need to handle exceptions, probably external to this method.
Personally, I've found Apache's HttpClient to be more than capable of everything I've needed to do with regards to this. Here is a great tutorial on using HttpClient
This is another java7 variant based on Brian Risk's answer with usage of try-with statement:
Give Java NIO a try:
Using
transferFrom()
is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.Check more about it here.
Note: The third parameter in transferFrom is the maximum number of bytes to transfer.
Integer.MAX_VALUE
will transfer at most 2^31 bytes,Long.MAX_VALUE
will allow at most 2^63 bytes (larger than any file in existence).You can do this in 1 line using netloader for Java: