Skip to content

Commit 909e820

Browse files
committed
update docs
1 parent e6dfd83 commit 909e820

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 3.6.00 (pending)
2+
* issue #336 Add ProgressMonitor for file downloads.
3+
14
## 3.5.00
25
* Re-package the object mapper sub-modules to work with Java 11 per issue #324.
36
* Update Jackson to 2.10.2

docs/index.md

+11
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,17 @@ File result = Unirest.get("http://some.file.location/file.zip")
394394
.getBody();
395395
```
396396

397+
### Download Progress Monitoring
398+
If you are uploading large files you might want to provide some time of progress bar to a user. You can monitor this progress by providing a ProgresMonitor.
399+
400+
```java
401+
Unirest.get("http://httpbin.org")
402+
.downLoadMonitor((b, fileName, bytesWritten, totalBytes) -> {
403+
updateProgressBarWithBytesLeft(totalBytes - bytesWritten);
404+
})
405+
.asFile("/disk/location/file.zip");
406+
```
407+
397408
## JSON responses
398409
Unirest offers a lightweight JSON response type when you don't need a full Object Mapper.
399410

unirest/src/main/java/kong/unirest/ProgressMonitor.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@
2727

2828
/**
2929
* A ProgressMonitor is a functional interface which can be passed to unirest for the purposes of
30-
* monitoring file uploads. A common use case is for drawing upload progress bars.
31-
* If the upload contains multiple files each one is called individually and the file name is provided.
30+
* monitoring file uploads and downloads. A common use case is for drawing progress bars.
31+
*
32+
* If an upload contains multiple files each one is called individually and the file name is provided.
3233
*
3334
* note that you will not receive a total for ALL files together at once.
34-
* If you wanted this you could keep track of the total bytes of files you planned to upload and then
35+
* If you wanted this you can keep track of the total bytes of files you planned to upload and then
3536
* have your ProgressMonitor aggregate the results.
3637
*/
3738
@FunctionalInterface
3839
public interface ProgressMonitor {
3940
/**
4041
* Accept stats about the current file upload chunk for a file.
41-
* @param field the field name
42+
* @param field the field name, or 'body' on file downloads
4243
* @param fileName the name of the file in question if available (InputStreams and byte arrays may not have file names)
43-
* @param bytesWritten the number of bytes that have been uploaded so far
44-
* @param totalBytes the total bytes that will be uploaded. Note this this may be an estimate if an InputStream was used
44+
* @param bytesWritten the number of bytes that have been uploaded or downloaded so far
45+
* @param totalBytes the total bytes that will be uploaded or downloaded.
46+
* On downloads this depends on the Content-Length header be returned
47+
* On uploads this this may be an estimate if an InputStream was used
4548
* */
4649
void accept(String field, String fileName, Long bytesWritten, Long totalBytes);
4750
}

0 commit comments

Comments
 (0)