Cannot write URLConnection because of doOutput

后端 未结 3 1842
独厮守ぢ
独厮守ぢ 2021-02-20 07:11

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 MalformedURLE         


        
相关标签:
3条回答
  • 2021-02-20 07:48

    The problem lies on this line:

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

    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());
    
    0 讨论(0)
  • 2021-02-20 07:59

    I am a Java programmer and I have a different solution. Maybe this will be useful for someone. Just have a look at your advanced proxy settings in your web browser. System engineers in our company had changed the proxy settings but I was not aware of it. This error cost me 3 work-days. I got this doOutput error while writing a ftp upload project in my company. I tried everything like adding conn.setDoOutput(true) or 'fifty shades' of similar solutions but non of them saved me. But, after I changed my proxy settings to correct ones, the error dissapeared and now I am able to upload my files through ftp using urlConnection in java. I used the code in the link below to make an upload process, and did not add anything except host, port, user and password. http://www.ajaxapp.com/2009/02/21/a-simple-java-ftp-connection-file-download-and-upload/

    0 讨论(0)
  • 2021-02-20 08:07

    For me it was due to incorrect way of using header, like

    HttpEntity<String> entity = new HttpEntity("parameters", headers); //Issue was here
    

    I just made as

     HttpEntity<String> entity = new HttpEntity(headers); //Fixed Issue
    

    check here other details

    0 讨论(0)
提交回复
热议问题