Java status 400 for URL





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Hello - I'm trying to download a file using Apache commons fileUtils but it always ends up getting a 400 error. The file's URL is valid because I successfully downloaded it many times using the browser. Any ideas?




java.io.IOException: Server returned HTTP response code: 400 for URL:
http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ
ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf at
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.URL.openStream(URL.java:1045) at
org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1478) at
com.nikaia.main.NikaiaReader.Downloader.download(Downloader.java:17)
at com.nikaia.main.NikaiaReader.Downloader.main(Downloader.java:32)




import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class Downloader {

public static void download(String url,String filename){

//System.out.println("filename is : "+filename);
try {
// FileUtils.copyURLToFile(new URL(url), new File("C:/downloads/"+filename));
FileUtils.copyURLToFile(new URL(url), new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));
try {
Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String ar){
download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
}
}









share|improve this question

























  • I tried it and now I get java.net.MalformedURLException : no protocol .. http%3A%2F%2Fwww.nikaia-hosp.gr%2Fimg%2F%C .... a very log string

    – chris_cs
    Nov 28 '17 at 14:10













  • download("nikaia-hosp.gr/img/"+URLEncoder.encode("ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf"),"stupid.pdf");

    – charlie
    Nov 28 '17 at 14:20











  • still no protocol ...

    – chris_cs
    Nov 28 '17 at 14:25











  • I think the bad request error is due to the whitespace in your url. Browser is able to handle this. in code we need to handle this

    – charlie
    Nov 28 '17 at 14:28




















1















Hello - I'm trying to download a file using Apache commons fileUtils but it always ends up getting a 400 error. The file's URL is valid because I successfully downloaded it many times using the browser. Any ideas?




java.io.IOException: Server returned HTTP response code: 400 for URL:
http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ
ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf at
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.URL.openStream(URL.java:1045) at
org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1478) at
com.nikaia.main.NikaiaReader.Downloader.download(Downloader.java:17)
at com.nikaia.main.NikaiaReader.Downloader.main(Downloader.java:32)




import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class Downloader {

public static void download(String url,String filename){

//System.out.println("filename is : "+filename);
try {
// FileUtils.copyURLToFile(new URL(url), new File("C:/downloads/"+filename));
FileUtils.copyURLToFile(new URL(url), new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));
try {
Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String ar){
download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
}
}









share|improve this question

























  • I tried it and now I get java.net.MalformedURLException : no protocol .. http%3A%2F%2Fwww.nikaia-hosp.gr%2Fimg%2F%C .... a very log string

    – chris_cs
    Nov 28 '17 at 14:10













  • download("nikaia-hosp.gr/img/"+URLEncoder.encode("ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf"),"stupid.pdf");

    – charlie
    Nov 28 '17 at 14:20











  • still no protocol ...

    – chris_cs
    Nov 28 '17 at 14:25











  • I think the bad request error is due to the whitespace in your url. Browser is able to handle this. in code we need to handle this

    – charlie
    Nov 28 '17 at 14:28
















1












1








1








Hello - I'm trying to download a file using Apache commons fileUtils but it always ends up getting a 400 error. The file's URL is valid because I successfully downloaded it many times using the browser. Any ideas?




java.io.IOException: Server returned HTTP response code: 400 for URL:
http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ
ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf at
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.URL.openStream(URL.java:1045) at
org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1478) at
com.nikaia.main.NikaiaReader.Downloader.download(Downloader.java:17)
at com.nikaia.main.NikaiaReader.Downloader.main(Downloader.java:32)




import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class Downloader {

public static void download(String url,String filename){

//System.out.println("filename is : "+filename);
try {
// FileUtils.copyURLToFile(new URL(url), new File("C:/downloads/"+filename));
FileUtils.copyURLToFile(new URL(url), new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));
try {
Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String ar){
download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
}
}









share|improve this question
















Hello - I'm trying to download a file using Apache commons fileUtils but it always ends up getting a 400 error. The file's URL is valid because I successfully downloaded it many times using the browser. Any ideas?




java.io.IOException: Server returned HTTP response code: 400 for URL:
http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ
ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf at
sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.URL.openStream(URL.java:1045) at
org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1478) at
com.nikaia.main.NikaiaReader.Downloader.download(Downloader.java:17)
at com.nikaia.main.NikaiaReader.Downloader.main(Downloader.java:32)




import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class Downloader {

public static void download(String url,String filename){

//System.out.println("filename is : "+filename);
try {
// FileUtils.copyURLToFile(new URL(url), new File("C:/downloads/"+filename));
FileUtils.copyURLToFile(new URL(url), new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));
try {
Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String ar){
download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
}
}






java apache file download






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 7:08









Robin Green

22.8k1076156




22.8k1076156










asked Nov 28 '17 at 13:47









chris_cschris_cs

85




85













  • I tried it and now I get java.net.MalformedURLException : no protocol .. http%3A%2F%2Fwww.nikaia-hosp.gr%2Fimg%2F%C .... a very log string

    – chris_cs
    Nov 28 '17 at 14:10













  • download("nikaia-hosp.gr/img/"+URLEncoder.encode("ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf"),"stupid.pdf");

    – charlie
    Nov 28 '17 at 14:20











  • still no protocol ...

    – chris_cs
    Nov 28 '17 at 14:25











  • I think the bad request error is due to the whitespace in your url. Browser is able to handle this. in code we need to handle this

    – charlie
    Nov 28 '17 at 14:28





















  • I tried it and now I get java.net.MalformedURLException : no protocol .. http%3A%2F%2Fwww.nikaia-hosp.gr%2Fimg%2F%C .... a very log string

    – chris_cs
    Nov 28 '17 at 14:10













  • download("nikaia-hosp.gr/img/"+URLEncoder.encode("ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf"),"stupid.pdf");

    – charlie
    Nov 28 '17 at 14:20











  • still no protocol ...

    – chris_cs
    Nov 28 '17 at 14:25











  • I think the bad request error is due to the whitespace in your url. Browser is able to handle this. in code we need to handle this

    – charlie
    Nov 28 '17 at 14:28



















I tried it and now I get java.net.MalformedURLException : no protocol .. http%3A%2F%2Fwww.nikaia-hosp.gr%2Fimg%2F%C .... a very log string

– chris_cs
Nov 28 '17 at 14:10







I tried it and now I get java.net.MalformedURLException : no protocol .. http%3A%2F%2Fwww.nikaia-hosp.gr%2Fimg%2F%C .... a very log string

– chris_cs
Nov 28 '17 at 14:10















download("nikaia-hosp.gr/img/"+URLEncoder.encode("ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf"),"stupid.pdf");

– charlie
Nov 28 '17 at 14:20





download("nikaia-hosp.gr/img/"+URLEncoder.encode("ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf"),"stupid.pdf");

– charlie
Nov 28 '17 at 14:20













still no protocol ...

– chris_cs
Nov 28 '17 at 14:25





still no protocol ...

– chris_cs
Nov 28 '17 at 14:25













I think the bad request error is due to the whitespace in your url. Browser is able to handle this. in code we need to handle this

– charlie
Nov 28 '17 at 14:28







I think the bad request error is due to the whitespace in your url. Browser is able to handle this. in code we need to handle this

– charlie
Nov 28 '17 at 14:28














2 Answers
2






active

oldest

votes


















0














OK answer found , I checked the encoded browser url and the url that UTF-8 java returns and the difference was that browser had %20 in the url but java had + .
I replaced all + with %20 in java and its working.



import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.commons.io.FileUtils;

public class Downloader {

public static void download(String url, String filename) {

try {

String base = "http://www.nikaia-hosp.gr/img/";
if (url.contains("http://www.nikaia-hosp.gr/img/")) {
FileUtils.copyURLToFile(
new URL(base + URLEncoder.encode(url.replace(base, ""), "UTF-8").replaceAll("\+", "%20")),
new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
} else {
FileUtils.copyURLToFile(new URL(url),
new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
}

try {
Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout")) * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String ar) {
download(
"http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf",
"stupid.pdf");
}
}





share|improve this answer































    0














    this work for me, the problem was the encoding, you need encode only the path of the url



    InputStream in = new URL(url).openStream();
    FileUtils.copyToFile(in,new File(filename));


    first open a Stream with the url and then copy this stream data in a file. using copyToFile method



    your code will be



    public static void download(String url,String filename){

    try {
    //changed this 2 lines
    URL encodeUrl = new URL(UriUtils.encodePath(url, "UTF-8"));
    InputStream in = encodeUrl.openStream();
    FileUtils.copyToFile(in, new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));

    try {
    Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static void main(String ar){
    download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
    }


    and add this dependency to your pom.xml



    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.0.4.RELEASE</version>
    </dependency>


    this does the magic.



    UriUtils.encodePath(host+path, "UTF-8");





    share|improve this answer


























    • Can you provide the full code ? tried to change these lines but i get 404

      – chris_cs
      Nov 28 '17 at 14:33











    • let me paste that

      – Jorge L. Morla
      Nov 28 '17 at 14:39











    • Not working Server returned HTTP response code: 400 for URL: nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

      – chris_cs
      Nov 28 '17 at 14:41











    • sorry change the url encoding and try again

      – Jorge L. Morla
      Nov 28 '17 at 15:22











    • But how i will get that url?? the url I have is nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

      – chris_cs
      Nov 28 '17 at 15:31














    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47533149%2fjava-status-400-for-url%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    OK answer found , I checked the encoded browser url and the url that UTF-8 java returns and the difference was that browser had %20 in the url but java had + .
    I replaced all + with %20 in java and its working.



    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;

    import org.apache.commons.io.FileUtils;

    public class Downloader {

    public static void download(String url, String filename) {

    try {

    String base = "http://www.nikaia-hosp.gr/img/";
    if (url.contains("http://www.nikaia-hosp.gr/img/")) {
    FileUtils.copyURLToFile(
    new URL(base + URLEncoder.encode(url.replace(base, ""), "UTF-8").replaceAll("\+", "%20")),
    new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
    } else {
    FileUtils.copyURLToFile(new URL(url),
    new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
    }

    try {
    Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout")) * 1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static void main(String ar) {
    download(
    "http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf",
    "stupid.pdf");
    }
    }





    share|improve this answer




























      0














      OK answer found , I checked the encoded browser url and the url that UTF-8 java returns and the difference was that browser had %20 in the url but java had + .
      I replaced all + with %20 in java and its working.



      import java.io.File;
      import java.io.IOException;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.net.URLEncoder;

      import org.apache.commons.io.FileUtils;

      public class Downloader {

      public static void download(String url, String filename) {

      try {

      String base = "http://www.nikaia-hosp.gr/img/";
      if (url.contains("http://www.nikaia-hosp.gr/img/")) {
      FileUtils.copyURLToFile(
      new URL(base + URLEncoder.encode(url.replace(base, ""), "UTF-8").replaceAll("\+", "%20")),
      new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
      } else {
      FileUtils.copyURLToFile(new URL(url),
      new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
      }

      try {
      Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout")) * 1000);
      } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      } catch (MalformedURLException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }

      public static void main(String ar) {
      download(
      "http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf",
      "stupid.pdf");
      }
      }





      share|improve this answer


























        0












        0








        0







        OK answer found , I checked the encoded browser url and the url that UTF-8 java returns and the difference was that browser had %20 in the url but java had + .
        I replaced all + with %20 in java and its working.



        import java.io.File;
        import java.io.IOException;
        import java.net.MalformedURLException;
        import java.net.URL;
        import java.net.URLEncoder;

        import org.apache.commons.io.FileUtils;

        public class Downloader {

        public static void download(String url, String filename) {

        try {

        String base = "http://www.nikaia-hosp.gr/img/";
        if (url.contains("http://www.nikaia-hosp.gr/img/")) {
        FileUtils.copyURLToFile(
        new URL(base + URLEncoder.encode(url.replace(base, ""), "UTF-8").replaceAll("\+", "%20")),
        new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
        } else {
        FileUtils.copyURLToFile(new URL(url),
        new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
        }

        try {
        Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout")) * 1000);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }

        public static void main(String ar) {
        download(
        "http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf",
        "stupid.pdf");
        }
        }





        share|improve this answer













        OK answer found , I checked the encoded browser url and the url that UTF-8 java returns and the difference was that browser had %20 in the url but java had + .
        I replaced all + with %20 in java and its working.



        import java.io.File;
        import java.io.IOException;
        import java.net.MalformedURLException;
        import java.net.URL;
        import java.net.URLEncoder;

        import org.apache.commons.io.FileUtils;

        public class Downloader {

        public static void download(String url, String filename) {

        try {

        String base = "http://www.nikaia-hosp.gr/img/";
        if (url.contains("http://www.nikaia-hosp.gr/img/")) {
        FileUtils.copyURLToFile(
        new URL(base + URLEncoder.encode(url.replace(base, ""), "UTF-8").replaceAll("\+", "%20")),
        new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
        } else {
        FileUtils.copyURLToFile(new URL(url),
        new File(PropertyReader.readProperty("ExtractedFilesPath") + "/" + filename));
        }

        try {
        Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout")) * 1000);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        } catch (MalformedURLException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        }

        public static void main(String ar) {
        download(
        "http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf",
        "stupid.pdf");
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '17 at 15:55









        chris_cschris_cs

        85




        85

























            0














            this work for me, the problem was the encoding, you need encode only the path of the url



            InputStream in = new URL(url).openStream();
            FileUtils.copyToFile(in,new File(filename));


            first open a Stream with the url and then copy this stream data in a file. using copyToFile method



            your code will be



            public static void download(String url,String filename){

            try {
            //changed this 2 lines
            URL encodeUrl = new URL(UriUtils.encodePath(url, "UTF-8"));
            InputStream in = encodeUrl.openStream();
            FileUtils.copyToFile(in, new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));

            try {
            Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            } catch (MalformedURLException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }

            public static void main(String ar){
            download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
            }


            and add this dependency to your pom.xml



            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.4.RELEASE</version>
            </dependency>


            this does the magic.



            UriUtils.encodePath(host+path, "UTF-8");





            share|improve this answer


























            • Can you provide the full code ? tried to change these lines but i get 404

              – chris_cs
              Nov 28 '17 at 14:33











            • let me paste that

              – Jorge L. Morla
              Nov 28 '17 at 14:39











            • Not working Server returned HTTP response code: 400 for URL: nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 14:41











            • sorry change the url encoding and try again

              – Jorge L. Morla
              Nov 28 '17 at 15:22











            • But how i will get that url?? the url I have is nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 15:31


















            0














            this work for me, the problem was the encoding, you need encode only the path of the url



            InputStream in = new URL(url).openStream();
            FileUtils.copyToFile(in,new File(filename));


            first open a Stream with the url and then copy this stream data in a file. using copyToFile method



            your code will be



            public static void download(String url,String filename){

            try {
            //changed this 2 lines
            URL encodeUrl = new URL(UriUtils.encodePath(url, "UTF-8"));
            InputStream in = encodeUrl.openStream();
            FileUtils.copyToFile(in, new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));

            try {
            Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            } catch (MalformedURLException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }

            public static void main(String ar){
            download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
            }


            and add this dependency to your pom.xml



            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.4.RELEASE</version>
            </dependency>


            this does the magic.



            UriUtils.encodePath(host+path, "UTF-8");





            share|improve this answer


























            • Can you provide the full code ? tried to change these lines but i get 404

              – chris_cs
              Nov 28 '17 at 14:33











            • let me paste that

              – Jorge L. Morla
              Nov 28 '17 at 14:39











            • Not working Server returned HTTP response code: 400 for URL: nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 14:41











            • sorry change the url encoding and try again

              – Jorge L. Morla
              Nov 28 '17 at 15:22











            • But how i will get that url?? the url I have is nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 15:31
















            0












            0








            0







            this work for me, the problem was the encoding, you need encode only the path of the url



            InputStream in = new URL(url).openStream();
            FileUtils.copyToFile(in,new File(filename));


            first open a Stream with the url and then copy this stream data in a file. using copyToFile method



            your code will be



            public static void download(String url,String filename){

            try {
            //changed this 2 lines
            URL encodeUrl = new URL(UriUtils.encodePath(url, "UTF-8"));
            InputStream in = encodeUrl.openStream();
            FileUtils.copyToFile(in, new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));

            try {
            Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            } catch (MalformedURLException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }

            public static void main(String ar){
            download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
            }


            and add this dependency to your pom.xml



            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.4.RELEASE</version>
            </dependency>


            this does the magic.



            UriUtils.encodePath(host+path, "UTF-8");





            share|improve this answer















            this work for me, the problem was the encoding, you need encode only the path of the url



            InputStream in = new URL(url).openStream();
            FileUtils.copyToFile(in,new File(filename));


            first open a Stream with the url and then copy this stream data in a file. using copyToFile method



            your code will be



            public static void download(String url,String filename){

            try {
            //changed this 2 lines
            URL encodeUrl = new URL(UriUtils.encodePath(url, "UTF-8"));
            InputStream in = encodeUrl.openStream();
            FileUtils.copyToFile(in, new File(PropertyReader.readProperty("ExtractedFilesPath")+"/"+filename));

            try {
            Thread.sleep(Integer.parseInt(PropertyReader.readProperty("downloadTimeout"))*1000);
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            } catch (MalformedURLException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }
            }

            public static void main(String ar){
            download("http://www.nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf","stupid.pdf");
            }


            and add this dependency to your pom.xml



            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.4.RELEASE</version>
            </dependency>


            this does the magic.



            UriUtils.encodePath(host+path, "UTF-8");






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '17 at 16:08

























            answered Nov 28 '17 at 14:29









            Jorge L. MorlaJorge L. Morla

            507312




            507312













            • Can you provide the full code ? tried to change these lines but i get 404

              – chris_cs
              Nov 28 '17 at 14:33











            • let me paste that

              – Jorge L. Morla
              Nov 28 '17 at 14:39











            • Not working Server returned HTTP response code: 400 for URL: nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 14:41











            • sorry change the url encoding and try again

              – Jorge L. Morla
              Nov 28 '17 at 15:22











            • But how i will get that url?? the url I have is nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 15:31





















            • Can you provide the full code ? tried to change these lines but i get 404

              – chris_cs
              Nov 28 '17 at 14:33











            • let me paste that

              – Jorge L. Morla
              Nov 28 '17 at 14:39











            • Not working Server returned HTTP response code: 400 for URL: nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 14:41











            • sorry change the url encoding and try again

              – Jorge L. Morla
              Nov 28 '17 at 15:22











            • But how i will get that url?? the url I have is nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

              – chris_cs
              Nov 28 '17 at 15:31



















            Can you provide the full code ? tried to change these lines but i get 404

            – chris_cs
            Nov 28 '17 at 14:33





            Can you provide the full code ? tried to change these lines but i get 404

            – chris_cs
            Nov 28 '17 at 14:33













            let me paste that

            – Jorge L. Morla
            Nov 28 '17 at 14:39





            let me paste that

            – Jorge L. Morla
            Nov 28 '17 at 14:39













            Not working Server returned HTTP response code: 400 for URL: nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

            – chris_cs
            Nov 28 '17 at 14:41





            Not working Server returned HTTP response code: 400 for URL: nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

            – chris_cs
            Nov 28 '17 at 14:41













            sorry change the url encoding and try again

            – Jorge L. Morla
            Nov 28 '17 at 15:22





            sorry change the url encoding and try again

            – Jorge L. Morla
            Nov 28 '17 at 15:22













            But how i will get that url?? the url I have is nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

            – chris_cs
            Nov 28 '17 at 15:31







            But how i will get that url?? the url I have is nikaia-hosp.gr/img/ΤΕΛΙΚΕΣ ΠΡΟΔΙΑΓΡΑΦΕΣ ΓΙΑ ΥΠΕΡΗΧΟ ΓΥΝΑΙΚΟΛΟΓΙΚΟ ΜΑΙΕΥΤΙΚΟ ΠΡΟΓΕΝΝΗΤΙΚΟΥ ΕΛΕΓΧΟΥ.pdf

            – chris_cs
            Nov 28 '17 at 15:31




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47533149%2fjava-status-400-for-url%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine