【问题标题】:File downloader for update system is crashing更新系统的文件下载器崩溃
【发布时间】:2012-04-15 22:42:17
【问题描述】:

我目前正在用 java 编写一个应用程序,将 mod 安装到 minecraft 中,并且更新程序/首次运行系统在尝试下载所需文件时崩溃,我收到以下异常

java.io.IOException: Access is denied
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(Unknown Source)
    at com.hachisoftware.mat.launcher.Util.getFileFromWeb(Util.java:43)
    at com.hachisoftware.mat.launcher.Main.setup(Main.java:22)
    at com.hachisoftware.mat.launcher.Main.main(Main.java:15)
java.io.FileNotFoundException: \Gui.jar (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at com.hachisoftware.mat.launcher.Util.getFileFromWeb(Util.java:71)
    at com.hachisoftware.mat.launcher.Util.getFileFromWeb(Util.java:45)
    at com.hachisoftware.mat.launcher.Main.setup(Main.java:22)
    at com.hachisoftware.mat.launcher.Main.main(Main.java:15)
java.io.IOException: Access is denied
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(Unknown Source)
    at com.hachisoftware.mat.launcher.Util.getFileFromWeb(Util.java:43)
    at com.hachisoftware.mat.launcher.Main.setup(Main.java:23)
    at com.hachisoftware.mat.launcher.Main.main(Main.java:15)
java.io.FileNotFoundException: \IntelliMod.jar (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at com.hachisoftware.mat.launcher.Util.getFileFromWeb(Util.java:71)
    at com.hachisoftware.mat.launcher.Util.getFileFromWeb(Util.java:45)
    at com.hachisoftware.mat.launcher.Main.setup(Main.java:23)
    at com.hachisoftware.mat.launcher.Main.main(Main.java:15)

我正在使用以下代码进行下载

public static boolean getFileFromWeb(String location, String destination)
{
    String[] s = location.split("/");
    String s2 = s[s.length - 1];

    if(!"".equals(destination) && !(new File(destination)).exists())
        (new File(destination)).mkdir();

    File outFile = new File(destination, s2);
    if(!outFile.exists())
        try { outFile.createNewFile(); } catch(Exception e) { e.printStackTrace(); }

    return getFileFromWeb(outFile, location);
}

public static boolean getFileFromWeb(File outFile, String location)
{
    BufferedInputStream in = null;
    BufferedOutputStream out = null;

    try
    {
        URL url = new URL(location);
        URLConnection urlc = url.openConnection();
        urlc.setRequestProperty("User-Agent", "HS File Downloader(" + clientName + ")");
        in = new BufferedInputStream(urlc.getInputStream());
        out = new BufferedOutputStream(new FileOutputStream(outFile));
        byte[] dataBuffer = new byte[4096 * 1024];

        for(int line = in.read(dataBuffer); line != -1; line = in.read(dataBuffer))
        {
            out.write(dataBuffer, 0, line);
            out.flush();
        }

        in.close();
        out.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return false;
    }

    return true;
}

欢迎任何帮助或建议

【问题讨论】:

    标签: java io download ioexception


    【解决方案1】:

    异常清楚地说明了

    java.io.IOException: Access is denied
    
    java.io.FileNotFoundException: \Gui.jar (Access is denied)
    

    确保您的程序有权访问文件系统。

    【讨论】:

    • 我知道,但它在之前就可以工作 如果我这样做,它就可以工作:Util.getFileFromWeb("http://mat.hachisoftware.com/engine/Gui.jar", "libs");Util.getFileFromWeb("http://mat.hachisoftware.com/engine/IntelliMod.jar", "libs");
    • 但不是Util.getFileFromWeb("http://mat.hachisoftware.com/engine/Gui.jar", ""); Util.getFileFromWeb("http://mat.hachisoftware.com/engine/IntelliMod.jar", "");
    • 我已经更改了我的代码并且现在正在工作,我正在将文件下载到启动器的同一文件夹中,我已将其更改为下载到子文件夹
    • 这是因为当您不提供任何参数时,我会尝试在作为根位置的 \Gui.jar 位置创建文件。因此,您可能无权访问它。
    猜你喜欢
    • 2019-10-20
    • 2017-04-29
    • 2015-09-05
    • 1970-01-01
    • 2020-05-10
    • 2019-10-31
    • 2021-12-26
    • 1970-01-01
    • 2017-06-24
    相关资源
    最近更新 更多