简体   繁体   English

在Java中下载文件时如何获取文件名?

[英]How to get file name while downloading a file in Java?

I have FileDownloader class that downloads the file from google drive and than these files can be used by other classes. 我有FileDownloader类,可以从google驱动器下载文件,然后这些文件可以由其他类使用。 I need also to extract filename somehow. 我还需要以某种方式提取文件名。 The problem is that this solution below works good for direct links, but it doesn't work when links are shorten with bit.ly for example... Could you please advise how I can change the code to get the right file name? 问题是下面的解决方案适用于直接链接,但是例如当使用bit.ly缩短链接时,该解决方案将不起作用...请您告知我如何更改代码以获取正确的文件名?

public class FileDownloader {

private static final int BUFFER_SIZE = 4096;


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

    // checking 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*=UTF-8''");

            if (index > 0) {
                fileName = disposition.substring(index + 17,
                        disposition.length());
            }
        } 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);
        }

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

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

Alternate Code to get File details from any HTTP URL using Java API: 使用Java API从任何HTTP URL获取文件详细信息的备用代码:

URL url = new URL(" http://www.example.com/somepath/filename.extension "); URL url =新URL(“ http://www.example.com/somepath/filename.extension ”);

    System.out.println(FilenameUtils.getBaseName(url.getPath())); 

// filename // 文档名称

    System.out.println(FilenameUtils.getName(url.getPath())); 

// filename.extension // filename.extension

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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