由于doOutput,无法编写URLConnection [英] Cannot write URLConnection because of doOutput

查看:338
本文介绍了由于doOutput,无法编写URLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了一个问题,并且由于用户问题我已经解决了我的问题。这是我目前使用的代码。

I've asked a question here and I've fixed my problem thanks to an user. This is the code I am using at the moment.

void UploadToDatabase() throws MalformedURLException, IOException {
          URL website = new URL("http://mk7vrlist.altervista.org/databases/record_file.txt");
          WritableByteChannel rbc = Channels.newChannel(website.openConnection().getOutputStream());

          FileOutputStream fos;
          fos = new FileOutputStream("record_file.txt");
          fos.getChannel().transferTo(0, Long.MAX_VALUE, rbc);
          fos.close();
     }

我的目标是上传文件 record_file.txt 在特定的网络链接上,如上所示。但是,当我尝试运行此代码时,NetBeans给了我这个错误:

My goal is upload the file record_file.txt on a particular web link as you can see above. However, when I try to run this code, NetBeans gives me this error:

java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)

我读了一些关于这个的东西我添加了声明 rbc 变量之后的代码。

I read some stuff about this and I added the following code just after the declaration of that rbc variable.

 URLConnection urlc = website.openConnection();
 urlc.setDoOutput(true);

顺便说一句,我总是有同样的错误。你能帮助我吗?

By the way, I always have the same error. Could you help me?

推荐答案

问题出在这一行:

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

您需要将 doOutput 设置为 true 为此工作。方法如下:

You will need to set doOutput to true for this to work. Here's how:

URLConnection urlc = website.openConnection();
urlc.setDoOutput(true);
WritableByteChannel rbc = Channels.newChannel(urlc.getOutputStream());

这篇关于由于doOutput,无法编写URLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆