无法从Dropbox下载文件

时间:2015-03-21 12:26:35

标签: java dropbox

我在dropbox上有一个公共文件存储空间,现在我想用java下载它。这就是我的方式:

   String url = "http://www.dropbox.com/s/vk67dz9ca0oqz37/Chrysanthemum.jpg";
        String filename = "C:\\Users\\Public\\Pictures\\Sample Pictures\\test.jpg";

        try {
            URL download = new URL(url);
            ReadableByteChannel rbc = Channels.newChannel(download.openStream());
            FileOutputStream fileOut = new FileOutputStream(filename);
            fileOut.getChannel().transferFrom(rbc, 0, 1 << 24);
            fileOut.flush();
            fileOut.close();
            rbc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

test.jpg无效。有什么问题?

1 个答案:

答案 0 :(得分:4)

当您点击&#34;下载原文&#34;在Dropbox页面中,您可以看到它将您重定向到http s ://www.dropbox.com/s/vk67dz9ca0oqz37/Chrysanthemum.jpg? dl = 1 < / p>

因此,请将?dl=1附加到您的网址并使用https

String url = "https://www.dropbox.com/s/vk67dz9ca0oqz37/Chrysanthemum.jpg?dl=1";
String filename = "C:\\Users\\Public\\Pictures\\Sample Pictures\\test.jpg";
try {
    URL download = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(download.openStream());
    FileOutputStream fileOut = new FileOutputStream(filename);
    fileOut.getChannel().transferFrom(rbc, 0, 1 << 24);
    fileOut.flush();
    fileOut.close();
    rbc.close();
} catch (Exception e) {
    e.printStackTrace();
}

或者,更短:

String url = "https://www.dropbox.com/s/vk67dz9ca0oqz37/Chrysanthemum.jpg?dl=1";
String filename = "C:\\Users\\Public\\Pictures\\Sample Pictures\\test.jpg";
try {
    URL download = new URL(url);
    Path fileOut = new File(filename).toPath();
    Files.copy(download.openStream(), fileOut, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
    e.printStackTrace();
}