/** * Downloads this attachment to given File * * @param file * The file, where the attachment will get downloaded to * * @return boolean true, if successful, otherwise false */ public boolean download(File file) { try { withInputStream((in) -> Files.copy(in, Paths.get(file.getAbsolutePath()))); return true; } catch (Exception e) { JDAImpl.LOG.error("Error while downloading an attachment", e); } return false; }
/** * Creates an {@link net.dv8tion.jda.core.entities.Icon Icon} instance for * this attachment if {@link #isImage() isImage()} is {@code true}! * <br>This is a convenience method that can be used to retrieve an Icon from an attachment image link which * requires a set user-agent to be loaded. * * <p>When a global proxy was specified via {@link net.dv8tion.jda.core.JDABuilder JDABuilder} this will use the * specified proxy to create an {@link java.io.InputStream InputStream} otherwise it will use a normal {@link java.net.URLConnection URLConnection} * with the User-Agent for the currently logged in account. * * @throws IOException * If an IOError occurs while reading the image * @throws java.lang.IllegalStateException * If this is not an image attachment * * @return {@link net.dv8tion.jda.core.entities.Icon Icon} for this image attachment * * @since 3.4.0 */ public Icon getAsIcon() throws IOException { if (!isImage()) throw new IllegalStateException("Cannot create an Icon out of this attachment. This is not an image."); AtomicReference<Icon> icon = new AtomicReference<>(); withInputStream((in) -> icon.set(Icon.from(in))); return icon.get(); }