java 如何使用Java在线下载mp3文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3653842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Java to download a mp3 file online?
提问by Frank
I used the following method to download an mp3 file at : http://online1.tingclass.com/lesson/shi0529/43/32.mp3
我使用以下方法下载 mp3 文件:http: //online1.tingclass.com/lesson/shi0529/43/32.mp3
But I got the following error :
但我收到以下错误:
java.io.FileNotFoundException: http:\online1.tingclass.com\lesson\shi0529\43\32.mp3 (The filename, directory name, or volume label syntax is incorrect)
java.io.FileNotFoundException: http:\online1.tingclass.com\lesson\shi0529\43\32.mp3(文件名、目录名或卷标语法不正确)
public static void Copy_File(String From_File,String To_File)
{
try
{
FileChannel sourceChannel=new FileInputStream(From_File).getChannel();
FileChannel destinationChannel=new FileOutputStream(To_File).getChannel();
sourceChannel.transferTo(0,sourceChannel.size(),destinationChannel);
// or
// destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
sourceChannel.close();
destinationChannel.close();
}
catch (Exception e) { e.printStackTrace(); }
}
Yet if I do it from a browser by hand, the file is there, I wonder why it didn't work, and what's the right way to do it ?
但是,如果我手动从浏览器执行此操作,文件就在那里,我想知道为什么它不起作用,正确的方法是什么?
Frank
坦率
回答by Joel
Using old-school Java IO, but you can map this to the NIO method you are using. Key thing is use of URLConnection.
使用老式 Java IO,但您可以将其映射到您正在使用的 NIO 方法。关键是使用 URLConnection。
URLConnection conn = new URL("http://online1.tingclass.com/lesson/shi0529/43/32.mp3").openConnection();
InputStream is = conn.getInputStream();
OutputStream outstream = new FileOutputStream(new File("/tmp/file.mp3"));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
回答by Roland Illig
When you create a FileInputStream
, you always access your local filesystem. Instead, you should use a URLConnection
for accessing files over HTTP.
创建 时FileInputStream
,您始终可以访问本地文件系统。相反,您应该使用 aURLConnection
来通过 HTTP 访问文件。
The indicator for this is that the forward slashes /
have turned into backward slashes \
.
这表明正斜杠/
已变成反斜杠\
。
回答by nivox
FileInputStream is used to access local files only. If you want to access the content of an URL you can setup an URLConnection or use something like this:
FileInputStream 仅用于访问本地文件。如果要访问 URL 的内容,可以设置 URLConnection 或使用以下内容:
URL myUrl = new URL("http://online1.tingclass.com/lesson/shi0529/43/32.mp3");
InputStream myUrlStream = myUrl.openStream();
ReadableByteChannel myUrlChannel = Channels.newChannel(myUrlStream);
FileChannel destinationChannel=new FileOutputStream(To_File).getChannel();
destinationChannel.transferFrom(myUrlChannel, 0, sizeOf32MP3);
Or more simply just make a BufferedInputStream from myUrlStream and cycle the read/write operation until EOF is found on myUrlStream.
或者更简单地从 myUrlStream 创建一个 BufferedInputStream 并循环读/写操作,直到在 myUrlStream 上找到 EOF。
Cheers, Andrea
干杯,安德里亚