我需要在窗口(JFrame
)上显示当前正在上传到dropbox的文件的上传进度。
我查看了多个示例,其中大部分都使用了dropbox API v1。
我需要使用API v2显示上传文件到Dropbox的进度。
我用于上传文件的编码如下所示:(如何操作此代码以显示文件上载进度。)
try
{
File file = new File("C:\\... Storage location of file on local system");
//reads file as input for the method to dropbox
InputStream fileupload = new FileInputStream(file);
//path in dropbox account to store the file
// if want to store it in root just put '/'
//if want to store file in a folder '/foldername/'
client.files().uploadBuilder("/" + file.getName())
.withMode(WriteMode.OVERWRITE)//always overwrites the existing file in the dropbox folder
.uploadAndFinish(fileupload);
JOptionPane.showMessageDialog(null, "File uploaded to dropbox");
}
//exception handled
catch (DbxException e)
{
//error message for uploading file to dropboxcloud
JOptionPane.showMessageDialog(null, "Unable to upload file to Cloud \n Error: " + e);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, "Unable to upload file to cloud \n Error: " + e);
}
当文件上传时,有人可以帮助我显示这个进度吗?
Dropbox API v2 Java SDK现在提供上传和下载的进度监听器。这已在v3.0.9中发布:
https://github.com/dropbox/dropbox-sdk-java/releases/tag/v3.0.9
这里有一个使用uploadAndFinish方法(https://dropbox.github.io/dropbox-sdk-java/api-docs/v3.0.x/com/dropbox/core/DbxUploader.html#uploadAndFinish-java.io.InputStream-long-com.dropbox.core.util.IOUtil.ProgressListener-)进行上传的例子:
它与文件下载的工作方式相同;下载方法(https://dropbox.github.io/dropbox-sdk-java/api-docs/v3.0.x/com/dropbox/core/DbxDownloader.html#download-java.io.OutputStream-com.dropbox.core.util.IOUtil.ProgressListener-)可选择以相同的方式获取ProgressListener参数。