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();
}