2014-10-04 49 views
2

下载文件:尝试使用我使用这个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(); 
    } 
} 

} 但我的问题是,当我试图从网址我得到下载此:

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直到文件下载?

回答

2

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

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

谢谢!还有另外1个问题是无论如何下载一个文件夹中的一切吗? – Emrage 2014-10-04 19:47:07