ByteChannel不上传文本文件

时间:2013-07-29 18:53:02

标签: java

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

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);
        }

1 个答案:

答案 0 :(得分: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();