Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.59k stars 593 forks source link

provide option for asFile() to overwrite a downloaded file if it exists #370

Closed gregkotsaftis closed 3 years ago

gregkotsaftis commented 3 years ago

When trying to download a file using your example code below, it fails without an exception if the file already exists.

File f = Unirest.get("http://mysite.com/mypic.jpg").asFile("mypic.jpg").getBody();

I use Java's File.createTempFile() method to create a unique tmp filename but since java creates an empty file as placeholder, when I pass that filename to asFile() it fails silently.

The problem seems to be in FileResponse.java, line 43 Files.copy(content, target); should be replaced with Files.copy(content, target,StandardCopyOption.REPLACE_EXISTING);

Of-course you should provide an option for this e.g. asFile(String path, boolean overwrite)

ryber commented 3 years ago

Rather than a bool we might as well expose the entire CopyOption interface in case there are other options folk want.

gregkotsaftis commented 3 years ago

even better! thnx

ryber commented 3 years ago

released in 3.11.00