2

我的程序必须上传用户选择的文本文件。我写了这个函数:

void UploadToDatabase() throws MalformedURLException, IOException {
          URL website = new URL("http://mk7vrlist.altervista.org/databases/test.txt");

          WritableByteChannel rbc = (WritableByteChannel) Channels.newChannel(website.openStream());
          FileOutputStream fos = new FileOutputStream("C:/recfile.txt");

          fos.getChannel().transferTo(0, Long.MAX_VALUE, rbc);
     }

我有一个错误,因为 IDE 告诉我“java.nio.channels.Channels$ReadableByteChannelImpl 不能转换为 java.nio.channels.WritableByteChannel”。

我该如何解决这个问题?我以这种方式调用该函数:(NetBeans 也向我建议)

try {
            UploadToDatabase();
        } catch (MalformedURLException ex) {
            Logger.getLogger(xGrep.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(xGrep.class.getName()).log(Level.SEVERE, null, ex);
        }
4

1 回答 1

1

我相信你正在寻找这个。

URL website = new URL("http://mk7vrlist.altervista.org/databases/test.txt");

WritableByteChannel rbc = Channels.newChannel(website.openConnection().getOutputStream());

FileOutputStream fos;
fos = new FileOutputStream("C:/recfile.txt");

fos.getChannel().transferTo(0, Long.MAX_VALUE, rbc);

fos.close();
于 2013-07-29T19:08:56.700 回答