2

我正在使用这个自动取款机:

package com.obisdian.downloader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;    
import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

/**
* Downloads the file, unzips the file, deletes the file
* @author Emre
*
*/
public class FileDownloader {

/** Boolean for of stuff is downloading */
public static boolean isDownloading;

/** The link of the file */
public final String fileLink = "https://dl.dropbox.com/s/tcm38xfgtxb5kve/client.jar?dl=0";

/** The file */
public final File file = new File(System.getProperty("user.home") + "/Obsidian");

/** The version of the file */
public final int version = 0;

/** The file version */
public final File versionFile = new File(file + "/version" + version);

/**
 * Checks of the file exists or not
 */
public void checkFile() {
    if(!file.exists()) {
        isDownloading = true;
        downloadFile();
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(versionFile));
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        isDownloading = false;
    }
    if(!versionFile.exists()) {
        deleteFile();
        isDownloading = true;
        downloadFile();
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(versionFile));
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/**
 * Downloads the file
 * @throws  
 */
public void downloadFile() {
    try {
        URL downloadUrl = new URL(fileLink);
        file.mkdirs();
        FileUtils.copyURLToFile(downloadUrl, file, 0, 0);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Deletes the file
 */
public void deleteFile() {
    try {
        FileUtils.deleteDirectory(file);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

但我的问题是,当我尝试从 url 下载时,我得到了这个:

java.io.IOException: File 'C:\Users\Emre\Obsidian' exists but is a directory
at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:354)
at org.apache.commons.io.FileUtils.openOutputStream(FileUtils.java:326)
at org.apache.commons.io.FileUtils.copyInputStreamToFile(FileUtils.java:1510)
at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1490)
at com.obisdian.downloader.FileDownloader.downloadFile(FileDownloader.java:70)
at com.obisdian.downloader.FileDownloader.checkFile(FileDownloader.java:39)
at com.obisdian.Game.start(Game.java:31)
at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

但是我的文件夹中有一个文件夹和一个版本文件。

那么我将如何让它下载我文件夹中的文件,如果可能的话我怎么能做到这一点:

FileUtils.copyURLToFile(downloadUrl, file, 0, 0);

停止执行 thread.sleep 直到文件下载?

4

1 回答 1

2

根据错误,Obsidian是一个目录 - 你需要创建一个新文件下载到:

public final File file = new File(System.getProperty("user.home") + "/Obsidian/FileToDownload");
于 2014-10-04T19:36:45.637 回答