-1

I have tried all the ways above to download from URL to file in java. All the ways work, they really downloading the files to the correct directory. There is one issue: The file content always in gibberish.

File content example:

"ãˇ¨ΩKìÏ»é&∂ü_˜l¥πUñëôÁ’ªnõ±QõFY˜’h°ë¡@A"Èx¸ëå∂˛Ô2w2y™n¡£Zã{Îdf<Hß;¯·fl˛”ß◊W—„Ãë¸Ùˇˆ…cdÈ?˝√”_?y:2ù¬ß¯˛flˇÎ˙‰)pG“8Ñ#:G1oÅߘÁœ›Àó?~’Ã( 8£èIÑÁ?~π‡ë{«–°$x{£u˘ûÑ•áÉz:íáÚgp8“¡k˝€;‰∞@óˆüüå[”E¡´N!Ø≤:h∞åw∏éψt∑$õWÇâ∞”SÄÁ˝◊?~[ O¬qåpDfl"4‡òfÙX˚ÖCÍÇ"

Is anyone has any idea why the file dowloaded in that way? Maybe I should try UTF-8 or something else?

My code:

    String dirName = "/Users/idanazulay/Desktop/Hotels-river/";
    String fileUrl = null;
    try {
        fileUrl = getHotelReview("en").getBody().getData().getURL();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    // downloadFileFromURLUsingNIO(dirName +"\\feed_en2.json", fileUrl);
    BufferedInputStream in = null;
    FileOutputStream fout = null;
    URL httpUrl = new URL(UriUtils.encodePath(fileUrl, "UTF-8"));
    try {
        try {
            in = new BufferedInputStream(httpUrl.openStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fout = new FileOutputStream(dirName + "\\feed_en2.json");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte data[] = new byte[1024];
        int count;
        try {
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } finally {
        if (in != null)
            in.close();
        if (fout != null)
            fout.close();
    }
    System.out.println("completed");
    return "Download completed";

}

// Using Commons IO library
public static void saveFileFromUrlWithCommonsIO(String fileName, String fileUrl)
        throws MalformedURLException, IOException {
    URL httpUrl = new URL(UriUtils.encodePath(fileUrl, "UTF-8"));
    FileUtils.copyURLToFile(httpUrl, new File(fileName));
}
private static void downloadFileFromURLUsingNIO(String fileName,String fileUrl) throws IOException {
      URL url = new URL(fileUrl);
      ReadableByteChannel rbc = Channels.newChannel(url.openStream());
      FileOutputStream fOutStream = new FileOutputStream(fileName);
      fOutStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
      fOutStream.close();
      rbc.close();
     }
     
     
6
  • 1
    Which kind of content type in file you are downloading please mention little more detail. Commented May 30, 2021 at 9:28
  • Code is working fine for normal json file which I tried to download over http and save it locally Can you share the file or content of actual file you are downloading Commented May 30, 2021 at 9:55
  • That "gibberish" looks like binary data. What are you downloading? Commented May 30, 2021 at 10:43
  • the file is Json file Commented May 31, 2021 at 16:46
  • You might be reading a stream with content-encoding gzip, so are writing compressed bytes to json file not the expanded text. Commented Jun 1, 2021 at 15:29

1 Answer 1

0

Solved The issue was that the file extension which I downloaded was (.gz - zip) . and I tried to copy the details while using "BufferedInputStream" - that was wrong because the file content are compressed. I switched between "BufferedInputStream" to "GZipInputStream" and the file download fine.

Thanks for everyone who helped :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.