Java 尝试使用FileUtils下载文件
我在用自动取款机:Java 尝试使用FileUtils下载文件,java,fileutils,Java,Fileutils,我在用自动取款机: 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
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直到下载文件?根据错误,黑曜石是一个目录-您需要创建一个新文件下载到:
public final File file = new File(System.getProperty("user.home") + "/Obsidian/FileToDownload");
谢谢还有一个问题,下载一个文件夹,里面有所有的东西吗?