DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Upload/Download a File To and From the Server
  • Step By Step Guide To Using Mule ESB
  • Internet Offline Scenario Automation on Android and iOS Mobile App
  • Recurrent Workflows With Cloud Native Dapr Jobs

Trending

  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • 5 Best Node.js Practices to Develop Scalable and Robust Applications
  • Solid Testing Strategies for Salesforce Releases
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  1. DZone
  2. Coding
  3. Java
  4. Java: How to Save / Download a File Available at a Particular URL Location on the Internet?

Java: How to Save / Download a File Available at a Particular URL Location on the Internet?

By 
Singaram Subramanian user avatar
Singaram Subramanian
·
Jan. 03, 12 · Interview
Likes (0)
Comment
Save
Tweet
Share
148.1K Views

Join the DZone community and get the full member experience.

Join For Free
package singz.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

/*
 * @author Singaram Subramanian
 */
public class FileDownloadTest {

public static void main(String[] args) {

 // Make sure that this directory exists
 String dirName = "C:\\FileDownload";

 try {

System.out.println("Downloading \'Maven, Eclipse and OSGi working together\' PDF document...");

 saveFileFromUrlWithJavaIO(
 dirName + "\\maven_eclipse_and_osgi_working_together.pdf",
 "http://singztechmusings.files.wordpress.com/2011/09/maven_eclipse_and_osgi_working_together.pdf");

 System.out.println("Downloaded \'Maven, Eclipse and OSGi working together\' PDF document.");

 System.out.println("Downloading \'InnoQ Web Services Standards Poster\' PDF document...");

 saveFileFromUrlWithCommonsIO(
 dirName + "\\innoq_ws-standards_poster_2007-02.pdf",
 "http://singztechmusings.files.wordpress.com/2011/08/innoq_ws-standards_poster_2007-02.pdf");

 System.out.println("Downloaded \'InnoQ Web Services Standards Poster\' PDF document.");

 } catch (MalformedURLException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }

}

// Using Java IO
 public static void saveFileFromUrlWithJavaIO(String fileName, String fileUrl)
 throws MalformedURLException, IOException {
 BufferedInputStream in = null;
 FileOutputStream fout = null;
 try {
 in = new BufferedInputStream(new URL(fileUrl).openStream());
 fout = new FileOutputStream(fileName);

byte data[] = new byte[1024];
 int count;
 while ((count = in.read(data, 0, 1024)) != -1) {
 fout.write(data, 0, count);
 }
 } finally {
 if (in != null)
 in.close();
 if (fout != null)
 fout.close();
 }
 }

// Using Commons IO library
 // Available at http://commons.apache.org/io/download_io.cgi
 public static void saveFileFromUrlWithCommonsIO(String fileName,
 String fileUrl) throws MalformedURLException, IOException {
 FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));
 }

}

 

From http://singztechmusings.wordpress.com/2011/12/20/java-how-to-save-download-a-file-available-at-a-particular-url-location-in-internet/

Java (programming language) Download Internet (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • How to Upload/Download a File To and From the Server
  • Step By Step Guide To Using Mule ESB
  • Internet Offline Scenario Automation on Android and iOS Mobile App
  • Recurrent Workflows With Cloud Native Dapr Jobs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!