java - 下载mp3文件而不是直接播放?
伊谢尔伦
伊谢尔伦 2017-04-17 13:02:50
0
2
418

在开发的过程中碰到一个问题,如下

现在需要做一个mp3文件下载的功能,但是在chrome浏览器下面总是直接播放,不知道怎么解决,求解答?

附加:有没有相关性的知识,能让我们对文件下载,对web服务输出格式,有个整体的认识?谢谢!

注:本人是学Java的,其它代码可能不怎么看的懂,不过您可以跟我说原理。
不管怎么样,感恩 ^_^

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
大家讲道理

http://tools.ietf.org/html/rfc2616#section-19.5.1

The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file.

An example is:

Content-Disposition: attachment; filename="fname.ext"
PHPzhong

Download mp3 files instead of playing them directly

Here is my original link: http://zcoder.cn/2015/01/01/Download mp3 files instead of playing them directly/

This article is suitable for everyone

Web engineers are suitable to read this article. Since I work in Java, the following codes are based on Java, but I will explain the basic principles

Requirements

In the browser, open a url whose output is an MP3 file stream. The requirement is to download it directly. However, there is a browser with its own playback function, and the browser will open the file directly.

Solution

Content-Disposition

rfc2616#section-19.5.1: http://tools.ietf.org/html/rfc2616#section-19.5.1
WIKI: http://en.wikipedia.org/wiki/MIME#Content-Disposition

What is Content-Disposition?, Content-disposition is an extension of the MIME protocol. The MIME protocol instructs the MIME user agent how to display attached files. Content-disposition can specify the file name when the user downloads it. In actual use, although Content-disposition can only specify a name, the browser will download it directly by default without specifying Content-Type; but if you do not specify Content-disposition, you can also download it by directly setting the response header Content-Type: application/octet-stream, but this The download does not specify a file name, and the file name downloaded by the user is the request path (Request URL).

Set response-header (response header)

Content-Disposition: attachment; filename="filename.ext"

Java code example

// 使客户端直接下载
// 响应的格式是:
// Content-Type: application/octet-stream
response.setContentType("application/octet-stream");

// 为客户端下载指定默认的下载文件名称
// 响应的格式是:
// Content-Disposition: attachment;filename="[文件名]"
response.setHeader("Content-Disposition",
        "attachment;filename=\"" + f.getName() + "\"");

If the request path is a static file

The problem is simple at this time. We only need to add a filter and set the response header Content-Type: application/octet-stream for the required request address.

Others

In the process of solving this problem, I found that there are many answers on stackoverflow.com, but I only found two pieces of information using Baidu, and the writing was not good. This shows two problems: 1. Baidu has 2. There is no such environment in China. Even if everyone solves the problem, they do not pay attention to accumulation or sharing.

The following is the answer on stackoverflow.com, it is very good, please share it.

http://stackoverflow.com/questions/3401650/stop-mp3-file-from-streaming-in-browsers/3401658#3401658

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template