使用 Java 从 URL 下载到文件(BufferedInputStream、FileOutputStream、Commons IO 库、NIO)
download from URL to file using Java (BufferedInputStream, FileOutputStream, Commons IO library, NIO)
我已经尝试了以上所有方法从URL下载到java中的文件。
所有方法都有效,他们确实将文件下载到正确的目录。有一个问题:
文件内容总是乱码
文件内容示例:
"ãˇ¨ΩKìÏ»é&∂ü_∼l¥πUñëôÁ'ªnõ±QõFY∼'h°ë¡@A"Èx¸ëå∂˛Ô2w2y™n¡£Zã{Èdf
有人知道为什么以这种方式下载文件吗?也许我应该试试 UTF-8 或其他格式?
我的代码:
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();
}
已解决
问题是我下载的文件扩展名是 (.gz - zip) 。
我尝试在使用“BufferedInputStream”时复制详细信息 - 这是错误的,因为文件内容被压缩了。
我在“BufferedInputStream”和“GZipInputStream”之间切换,文件下载正常。
感谢所有帮助过的人:)
我已经尝试了以上所有方法从URL下载到java中的文件。 所有方法都有效,他们确实将文件下载到正确的目录。有一个问题: 文件内容总是乱码
文件内容示例:
"ãˇ¨ΩKìÏ»é&∂ü_∼l¥πUñëôÁ'ªnõ±QõFY∼'h°ë¡@A"Èx¸ëå∂˛Ô2w2y™n¡£Zã{Èdf 有人知道为什么以这种方式下载文件吗?也许我应该试试 UTF-8 或其他格式? 我的代码: 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();
}
已解决 问题是我下载的文件扩展名是 (.gz - zip) 。 我尝试在使用“BufferedInputStream”时复制详细信息 - 这是错误的,因为文件内容被压缩了。 我在“BufferedInputStream”和“GZipInputStream”之间切换,文件下载正常。
感谢所有帮助过的人:)