[英]download from URL to file using Java (BufferedInputStream, FileOutputStream, Commons IO library, NIO)
I have tried all the ways above to download from URL to file in java.我已经尝试了上述所有方法从 URL 下载到 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ÍÇ" "ãˇ¨ΩKìÏ»é&∂ü_~l¥πUñëôÁ'ªnõ±QõFY~'h°ë¡@A"Èx¸ëå∂˛Ô2w2y™n¡£Zã{Îdf<Hß;¯·f˛"ß◊W— „Ãë¸Ùˇˆ…cdÈ?˝√”_?y:2ù¬ß¯˛fˇÎ˙‰)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?
也许我应该尝试 UTF-8 或其他什么?
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();
}
Solved The issue was that the file extension which I downloaded was (.gz - zip).已解决问题是我下载的文件扩展名是(.gz - zip)。 and I tried to copy the details while using "BufferedInputStream" - that was wrong because the file content are compressed.
我尝试在使用“BufferedInputStream”时复制详细信息 - 这是错误的,因为文件内容被压缩。 I switched between "BufferedInputStream" to "GZipInputStream" and the file download fine.
我在“BufferedInputStream”和“GZipInputStream”之间切换,文件下载正常。
Thanks for everyone who helped:)感谢所有帮助过的人:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.