SoFunction
Updated on 2025-03-02

How to download remote files to local directories in Java

Preface

I encountered a need to download attachments during development today

His attachment is that when a query exists in a network disk, it just returns me a path of https.

You need to download the attachment to the local directory through this path

1. Introduction to the text

import .*;
import ;
import ;
import ;
import ;

/**
  * @Author
  * @Date
  * @Version 1.0
  * <p>Note: Download remote files to local. Choose one of two methods.
  */
public class downloadUtil {

    /**
      * Download remote files and save them locally
      *
      * @param remoteFilePath - Remote file path
      * @param localFilePath - local file path (with file name)
      */
    public static void downloadFile1(String remoteFilePath, String localFilePath) {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(localFilePath);
        try {
            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection) ();
            ();
            bis = new BufferedInputStream(());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = (b)) != -1) {
                (b, 0, len);
            }
            ();
            ();
            ();
        } catch (Exception e) {
            ();
        } finally {
            try {
                ();
                ();
            } catch (IOException e) {
                ();
            }
        }
    }

    /**
      * Download remote files and save them locally
      *
      * @param remoteFilePath - Remote file path
      * @param localFilePath - local file path (with file name)
      */
    public static void downloadFile2(String remoteFilePath, String localFilePath) {
        URL website = null;
        ReadableByteChannel rbc = null;
        FileOutputStream fos = null;
        try {
            website = new URL(remoteFilePath);
            rbc = (());
            fos = new FileOutputStream(localFilePath);//Local file address:            ().transferFrom(rbc, 0, Long.MAX_VALUE);
        } catch (Exception e) {
            ();
        }finally{
            if(fos!=null){
                try {
                    ();
                } catch (IOException e) {
                    ();
                }
            }
            if(rbc!=null){
                try {
                    ();
                } catch (IOException e) {
                    ();
                }
            }
        }
    }


    /**
      * A small test
      * @param args
      */
    public static void main(String[] args) {
        /*Remote file path*/
        String remoteFilePath1 = "/creative/vcg/800/new/";
        String remoteFilePath2 = "https://pic./2019/1112/";
        /*Local file path (with file name)*/
        String localFilePath1 ="E:\\LeStoreDownload\\update\\Guangzhou Tower.jpg";
        String localFilePath2 ="E:\\LeStoreDownload\\update\\Bridge.jpg";
        downloadFile1(remoteFilePath1,localFilePath1);
        downloadFile2(remoteFilePath2,localFilePath2);
    }

}

2. Test introduction

Here I used the image path I searched online for testing for reference only, as the main text introduction

Summarize

This is the end of using Java to implement remote files downloaded to local directory records

The above is personal experience. I hope you can give you a reference and I hope you can support me more.