Java文件下载程序不工作

Java文件下载程序不工作,java,Java,我已经创建了这个函数 void DownloadFromDatabase() throws IOException { URL website = new URL("http://theurlofmywebsite.org/databases/record_file.txt"); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); Fil

我已经创建了这个函数

void DownloadFromDatabase() throws IOException {
            URL website = new URL("http://theurlofmywebsite.org/databases/record_file.txt");

            ReadableByteChannel rbc = Channels.newChannel(website.openStream());
            FileOutputStream fos = new FileOutputStream("record_file.txt");

            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        }
。。。当我点击一个按钮的时候,我会叫它,你可以在这里看到

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try {

            DownloadFromDatabase();

        } catch (IOException ex) {

           Logger.getLogger(xGrep.class.getName()).log(Level.SEVERE, null, ex);

        }
}

单击按钮时,
DownloadFromDatabase(),但我在桌面上看不到文件
record\u file.txt
。你知道为什么吗?

这段代码不是最好的,但我已经在我的电脑上做了一个测试,它工作正常。它在2秒内下载一个500行的文本文件

void DownloadFromDatabase() throws MalformedURLException, IOException {

     URLConnection conn = new URL("your_url_here").openConnection();

     InputStream is = conn.getInputStream();
     OutputStream outstream = new FileOutputStream(new File("filename.txt"));

     byte[] buffer = new byte[4096];
     int len;

     while ((len = is.read(buffer)) > 0) {
      outstream.write(buffer, 0, len);
     }
     outstream.close();
     }

我将其命名为
DownloadFromDatabase()
,因此您只需复制/粘贴此代码,而不必复制/粘贴您的代码。另外,请注意异常情况。

否-但是当您吞下所有
IOException
s而根本不记录它们时,我猜一个异常正在发生,而您忽略了它。你几乎不应该接受这样的例外情况。(此外,您应该遵循Java命名约定,并更容易地缩进代码。)然后呢?我能对代码做些什么呢?一旦你记录了异常,你就会更好地了解出了什么问题。例如,可能您没有对本地文件的写入权限。或者它在HTTP传输中失败。它在哪里下载文件?在你想要的地方。看看这里:新文件(“filename.txt”)。键入例如C://folder1//folder2//filename.txt以获取所需的路径:)