gpt4 book ai didi

java - 尝试使用 FileUtils 下载文件

转载 作者:行者123 更新时间:2023-11-30 09:01:53 24 4
gpt4 key购买 nike

我正在使用这个 atm:

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)

但是我的文件夹里有一个文件夹和一个versionfile。

那么我该如何让它下载我文件夹中的文件,如果可能的话我该怎么做:

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

在文件下载之前停止执行 thread.sleep?

最佳答案

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

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

关于java - 尝试使用 FileUtils 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com