简体   繁体   中英

How to read all files and folders “recursively” through a url link

I'm trying to download all files from a URL. But, analyzing the code through the chrome developer tools, I can see that the files I need are in a path as example: /var/www/html/folders_and_files_that_i_need .

Someone have any idea to help me? Thanks in advance.

My code actually can only download the file setting absolute path.

public static void downloadFile(String fileURL, String saveDir) throws IOException {
    URL url = new URL(fileURL);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    int responseCode = httpConn.getResponseCode();

    // always check HTTP response code first
    if (responseCode == HttpURLConnection.HTTP_OK) {
        String fileName = "";
        String disposition = httpConn.getHeaderField("Content-Disposition");
        String contentType = httpConn.getContentType();
        int contentLength = httpConn.getContentLength();

        if (disposition != null) {
            // extracts file name from header field
            int index = disposition.indexOf("filename=");
            if (index > 0) {
                fileName = disposition.substring(index + 10, disposition.length() - 1);
            }
        } else {
            // extracts file name from URL
            fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
        }

        System.out.println("Content-Type = " + contentType);
        System.out.println("Content-Disposition = " + disposition);
        System.out.println("Content-Length = " + contentLength);
        System.out.println("fileName = " + fileName);

        // opens input stream from the HTTP connection
        InputStream inputStream = httpConn.getInputStream();
        String saveFilePath = saveDir + File.separator + fileName;

        // opens an output stream to save into file
        FileOutputStream outputStream = new FileOutputStream(saveFilePath);

        int bytesRead = -1;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
            System.out.println("buffering");
        }

        outputStream.close();
        inputStream.close();

        System.out.println("Arquivo " + fileName + " baixado");
    } else {
        System.out.println("No file to download. Server replied HTTP code: " + responseCode);
    }
    httpConn.disconnect();
}

In REST API u have to have a resource for individual files Java download all files and folders in a directory

may help you out.

You can use below logic:

  1. Get All full file path inside that folder using recursive algorithm.
  2. In your servlet, you can zip all of the files.
  3. Write the zipped file in ServletOutputStream

Below is the explanation for this:

Get All full file path inside that folder using recursive algorithm.

You can use below code given for getting all files inside a folder using recursion, this code has been taken from here

// Recursive Java program to print all files
// in a folder(and sub-folders)

import java.io.File;

public class GFG 
{
     static void RecursivePrint(File[] arr,int index,int level) 
     {
         // terminate condition
         if(index == arr.length)
             return;

         // tabs for internal levels
         for (int i = 0; i < level; i++)
             System.out.print("\t");

         // for files
         if(arr[index].isFile())
             System.out.println(arr[index].getName());

         // for sub-directories
         else if(arr[index].isDirectory())
         {
             System.out.println("[" + arr[index].getName() + "]");

             // recursion for sub-directories
             RecursivePrint(arr[index].listFiles(), 0, level + 1);
         }

         // recursion for main directory
         RecursivePrint(arr,++index, level);
    }

    // Driver Method
    public static void main(String[] args)
    {
        // Provide full path for directory(change accordingly)  
        String maindirpath = "C:\\Users\\Gaurav Miglani\\Desktop\\Test";

        // File object
        File maindir = new File(maindirpath);

        if(maindir.exists() && maindir.isDirectory())
        {
            // array for files and sub-directories 
            // of directory pointed by maindir
            File arr[] = maindir.listFiles();

            System.out.println("**********************************************");
            System.out.println("Files from main directory : " + maindir);
            System.out.println("**********************************************");

            // Calling recursive method
            RecursivePrint(arr,0,0); 
       } 
    }
}

For making zip and downloading it from servlet you can go through this link Using a servlet, how do you download multiple files from a database and zip them for client download

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM