简体   繁体   English

从Java Servlet Web应用程序将文件上传到我的保管箱

[英]Upload a file to my dropbox from a java servlet web-application

I would like to use my webapp to create a file based on user input, and upload it to my dropbox. 我想使用我的webapp根据用户输入创建文件,并将其上传到我的保管箱。 For some reason there is no tutorial for java on the Dropbox website. 由于某些原因,Dropbox网站上没有适用于Java的教程。 I've found two examples: 我发现了两个例子:
example1 例子1
example2 . example2

The latter is from May 2013, however, both seem to use an old API, which is no longer supported. 后者来自2013年5月,但是,两者似乎都使用了旧的API,因此不再受支持。 I'M looking for a working example using dropbox-core-sdk-1.7.jar. 我正在寻找一个使用dropbox-core-sdk-1.7.jar的工作示例。

You might take a look at the examples folder in the Java SDK to see how the API differs from earlier versions. 您可以查看Java SDK中的examples文件夹,以了解API与早期版本的不同之处。 Also take a look at the documentation. 另请参阅文档。 For example, you'll probably want to use uploadFile : http://dropbox.github.io/dropbox-sdk-java/api-docs/v1.7.x/com/dropbox/core/DbxClient.html#uploadFile(java.lang.String , com.dropbox.core.DbxWriteMode, long, java.io.InputStream). 例如,您可能要使用uploadFilehttp : //dropbox.github.io/dropbox-sdk-java/api-docs/v1.7.x/com/dropbox/core/DbxClient.html#uploadFile( java.lang.String ,com.dropbox.core.DbxWriteMode,long,java.io.InputStream)。

(Sorry; I can't quite get that URL to parse properly.) (抱歉;我无法完全理解该网址。)

First create a DbxClient (shown in the examples), then upload: 首先创建一个DbxClient(如示例所示),然后上传:

/**
* Upload a file to your Dropbox
* @param srcFilename path to the source file to be uploaded 
*  e.g. /tmp/upload.txt
* @param destFilename path to the destination.
*  Must start with '/' and must NOT end with'/' 
*  e.g. /target_dir/upload.txt 
* @param dbxClient a DbxClient created using an auth-token for your Dropbox app
* @throws IOException 
*/
public void upload (String srcFilename, String destFilename, DbxClient dbxClient) throws IOException {

    File uploadFile = new File (srcFilename);
    FileInputStream uploadFIS;
    try {
        uploadFIS = new FileInputStream(uploadFile);
    }

    catch (FileNotFoundException e1) {          
        e1.printStackTrace();
        System.err.println("Error in upload(): problem opening " + srcFilename);
        return;
    }

    String targetPath = destFilename; 
    try {
        dbxClient.uploadFile(targetPath, DbxWriteMode.add(), uploadFile.length(), uploadFIS);
    } 

    catch (DbxException e) {
        e.printStackTrace();
        uploadFIS.close();
        System.err.println("Error in upload(): " + e.getMessage());
        System.exit(1);
        return;         
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM