Download binary file from Github using Java

后端 未结 5 1203
北海茫月
北海茫月 2021-01-05 11:12

I\'m trying to download this file (http://github.com/downloads/TheHolyWaffle/ChampionHelper/ChampionHelper-4.jar) with the following method and it doesn\'t seem to work. I\'

5条回答
  •  粉色の甜心
    2021-01-05 11:48

    Get the direct download link to the raw binary file e.g. https://github.com/xerial/sqlite-jdbc/blob/master/src/main/resources/org/sqlite/native/Windows/x86_64/sqlitejdbc.dll?raw=true by copying the View Raw link:

    Finally use the following piece of code to download the file:

    public static void download(String downloadURL) throws IOException
    {
        URL website = new URL(downloadURL);
        String fileName = getFileName(downloadURL);
    
        try (InputStream inputStream = website.openStream())
        {
            Files.copy(inputStream, Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);
        }
    }
    
    public static String getFileName(String downloadURL)
    {
        String baseName = FilenameUtils.getBaseName(downloadURL);
        String extension = FilenameUtils.getExtension(downloadURL);
        String fileName = baseName + "." + extension;
    
        int questionMarkIndex = fileName.indexOf("?");
        if (questionMarkIndex != -1)
        {
            fileName = fileName.substring(0, questionMarkIndex);
        }
    
        fileName = fileName.replaceAll("-", "");
        return URLDecoder.decode(fileName, "UTF-8");
    }
    

    You will also need the Apache Commons IO maven dependency for the FilenameUtils class:

    
        commons-io
        commons-io
        LATEST
    
    

提交回复
热议问题