TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
在java中,有上传和下载文件的接口,同样的,在JavaFX中也有文件上传和下载功能。这是通过JavaFX1.2中的一个新属性--HttpRequest来实现的。
HttpRequest拥有input和onInput属性,这可以提供InputStream来用于下载数据。这个stream支持mark,reset,available,skip等。但是不支持HttpRequest缓冲这些数据。因为缓冲这些数据不能使用这个方法去下载大型文件。
JavaFX1.2引入了两个新属性--source(InputStream)和sink(OutputStream)。 如果我们设置sink属性,我们可以直接下载stream中的内容,而用不着buffering。同样的,我们用source来上传内容。当source和sink被使用后,相应的input,onInput,output,onOutput将不再起作用。

这个应用程序将下载一个大尺寸照片。- function downloadFile(url , outputFile) {
-
- def getRequest: HttpRequest = HttpRequest {
-
- location: url
- [b]sink: [/b][b][b]new [/b]java.io.FileOutputStream(outputFile)[/b]
-
- onToRead: function(bytes: Long) {
- toRead = bytes;
- println("onToRead({bytes})");
- }
-
- onRead: function(bytes: Long) {
- read = bytes;
- println("onRead - {read * 100/toRead}%");
- }
-
- onDone: function() { println("onDone") }
- }
-
- getRequest.start();
- }
-
-
- 在上面的例子中,sink被指派到了FileOutputStream中。所有的内容将被写入文件里而不是buffering。
- 注意:为了使这个应用程序与moblie或者其他平台兼容,我们将不得不使用java.io.package的子集:[url=http://java.sun.com/javame/reference/apis/jsr118/java/io/package-summary.html][b]MID Profile Core API[/b][/url].
复制代码
源码下载:http://file.javaxxz.com/2014/10/12/234939140.zip |
|