如何阅读所有文件和文件夹"递归"通过网址链接

时间:2018-05-24 18:58:10

标签: java php bash file url

我试图从网址下载所有文件。但是,通过chrome开发人员工具分析代码,我可以看到我需要的文件在路径中作为示例: /var/www/html/folders_and_files_that_i_need

有人有任何想法可以帮助我吗?提前谢谢。

我的代码实际上只能下载文件设置绝对路径。

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();
}

2 个答案:

答案 0 :(得分:2)

在REST API中,您必须拥有单个文件的资源 Java download all files and folders in a directory

可能会帮助你。

答案 1 :(得分:1)

您可以使用以下逻辑:

  1. 使用递归算法获取该文件夹中的所有完整文件路径。
  2. 在您的servlet中,您可以压缩所有文件。
  3. 将压缩文件写入ServletOutputStream
  4. 以下是对此的解释:

    使用递归算法获取该文件夹中的所有完整文件路径。

    您可以使用下面的代码来获取使用递归获取文件夹中的所有文件,此代码已被采用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); 
           } 
        }
    }
    

    要制作zip并从servlet下载,您可以浏览此链接 Using a servlet, how do you download multiple files from a database and zip them for client download