Search code examples
javahttpurlconnection

Is there a way to download a .jar file with a httpurlconnection?


The server is sending a jar file and i want to save it in a specific directory. My HttpURLConnection is only reading the content of the file

public static String update(String url, String version) throws MalformedURLException, IOException {
    HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
        
    return readResponse(con.getInputStream());
}
private static String readResponse(InputStream in){
    BufferedReader br = new BufferedReader(new InputStreamReader((in)));
    StringBuilder sb = new StringBuilder();
    String output;
    while ((output = br.readLine()) != null) {
        sb.append(output);
    }
    return sb.toString();
}

Solution

  • Why are u parsing it as a string? Maybe try (https://www.baeldung.com/java-download-file)

    InputStream in = new URL(FILE_URL).openStream();
    Files.copy(in, Paths.get(FILE_NAME), StandardCopyOption.REPLACE_EXISTING);