在使用java使用dropbox选择器从dropbox中提取文件后,如何将文件存储在服务器中

How can I store the files in my server after picking the files from dropbox with the dropbox Chooser using java

本文关键字:文件 dropbox 服务器 存储 选择器 java 提取 使用      更新时间:2023-09-26

我的站点中有一个表单。表单允许用户从本地机器中选择文件,或从下拉框中选择文件。我知道如何从本地机器获取文件,然后通过做一些PHP的事情将其存储在服务器中。

我对Dropbox做了一项研究,发现他们做了一种名为"Chooser-Dropbox"的东西。(基本上,它是一个小的JavaScript组件,使我们的网络应用程序能够从Dropbox中获取文件),而且我们可以在网络中集成"Chooser",这非常令人惊讶。

但我的问题是,在用选择器从dropbox中提取文件后,我不知道如何将文件存储在我的服务器中。(我基本上想下载该文件,并在从Chooser中选择文件后存储在我的服务器中)。

这是DropBox希望我们在我们的网络中放置的java脚本,用于Chooser 的工作

<script type="text/javascript" src="https://www.dropbox.com/static/api/2/dropins.js" id="dropboxjs" data-app-key="YOUR_APP_KEY"></script>

从JavaScript:触发选择器

var button = Dropbox.createChooseButton(options);
document.getElementById("container").appendChild(button);

以下是选项:

options = {
    // Required. Called when a user selects an item in the Chooser.
    success: function(files) {
        alert("Here's the file link: " + files[0].link)
    },
    cancel: function() {
    },
    linkType: "direct", // or "direct"
    multiselect: false, // or true
    extensions: ['.pdf', '.doc', '.docx'],
};

在成功回调中,我有一个文件的下载链接,我们从dropbox中选择了它。

我在前端使用jquery,在后端使用spring。

将下载链接传递到java端,我们可以将dropbox文件下载到本地机器,然后我可以再次将其上传到应用程序服务器。

但有没有一种方法可以将文件上传到应用服务器,而无需将文件下载到本地机器。

请帮帮我。

在成功块中,我得到了文件下载链接。

 success: function(files) {
        alert("Here's the file link: " + files[0].link)
    }

再次使用ajax,我将这个下载链接传递到springcontroller,从控制器传递到服务类。

在服务类中,使用以下代码,我们可以将文件保存到服务器路径。

        File newFile = new File("here write server path");
        if(!newFile.exists()){
            URL url = new URL(downloadLink);
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            FileOutputStream fos = new FileOutputStream(newFile);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            fos.close();
            rbc.close();
        }