-2

I want to add a URL into my java program: http://www.markit.com/news/InterestRates_JPY_20160426.zip; so basically when you open this link a zip file is downloaded. How do I do that?

And then, I want to unzip the downloaded file in the java program as well.

How do I do these in java?

user3591433
  • 105
  • 1
  • 3
  • 11

1 Answers1

0

You can use zip4j to unzip your file.

To download a file in Java you can use this code.

try
{
    String url = "download url";
    String path = "C:/Users/...."; // Path to where the files is going to be downloaded.
    ReadableByteChannel in = Channels.newChannel( new URL(url).openStream() );
    FileOutputStream fileOutputStream = new FileOutputStream(path);
    FileChannel out = fileOutputStream.getChannel();
    out.transferFrom(in, 0, Long.MAX_VALUE);
}
catch (Exception e)
{
    e.printStackTrace();
}
bhazero025
  • 337
  • 4
  • 15
  • I'm getting this error: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at the ReableByteChannel line – user3591433 May 19 '16 at 13:40