Java ByteChannel未上载文本文件
我的程序必须上传用户选择的文本文件。我写了这个函数:Java ByteChannel未上载文本文件,java,Java,我的程序必须上传用户选择的文本文件。我写了这个函数: void UploadToDatabase() throws MalformedURLException, IOException { URL website = new URL("http://mk7vrlist.altervista.org/databases/test.txt"); WritableByteChannel rbc = (WritableByteChannel) Channels.
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.writeablebytechannel”
我如何解决这个问题?我这样调用函数:(NetBeans也向我建议)
我相信你在找这个
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();
这会在我的服务器上上传“recfile.txt”吗+1您有一个FileOutputStream(“C:/recfile.txt”),因此我相信您正在将其写入该位置。“C:/recfile.txt”不,我只想把“recfile.txt”上传到服务器上。我不需要在里面写任何东西:)更新。试试看,太好了!你能不能再问一个问题,我来查一查。如果你能把问题写出来,告诉我们哪里出了错误,那么理解问题就容易多了。
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();