2014-10-04 55 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