问题
I am working on a Spring-MVC application in which I am trying to stream mp3 data. Unfortunately, it's directly triggering a download whenever a byte-array of information is sent in response. I found a few other links, but most of them are tied to an user-interface, so not much useful. What are the requirements for streaming a mp3 file? Here is the download code I have.
@RequestMapping(value = "/getsong/{token}")
public ResponseEntity<byte[]> getsong(@PathVariable("token") String token, HttpServletResponse response) {
try {
Path path = Paths.get(FILE_LOCATION);
response.setContentType("audio/mp3");
response.setHeader("Content-Disposition", "attachment; filename=\"" + "song.mp3" + "\"");
response.setContentLength((int) Files.size(path));
Files.copy(path, response.getOutputStream());
response.flushBuffer();
} catch (Exception ignored) {
}
return null;
}
Thank you.
回答1:
You may have resolved this by now. But still just wanted to add my comment here - you are setting 'attachment' to Content-Disposition header. I think that's what causing it to download every time you call this code.
回答2:
You don't mention what your client/player is but most players are going to require a controller that supports partial content requests (or byte ranges).
This can be a little tricky to implement so I would suggest using something like Spring Content. Then you don't need to worry about how implement the controller code at all. Assuming you are using Spring Boot (let me know if you are not) then it would look something like this:
pom.xml
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>content-fs-spring-boot-starter</artifactId>
<version>0.0.10</version>
</dependency>
SpringBootApplication.java
@SpringBootApplication
public class YourSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(YourSpringBootApplication.class, args);
}
@Configuration
@EnableFilesystemStores
public static class StoreConfig {
File filesystemRoot() {
return new File("/path/to/your/songs");
}
@Bean
public FileSystemResourceLoader fsResourceLoader() throws Exception {
return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
}
}
@StoreRestResource(path="songs")
public interface SongsStore extends Store<String> {
//
}
}
This is all you need to create a REST-based movie service at /songs
supporting streaming. It actually supports full CRUD functionality as well; Create == POST, Read == GET (include byte-range support), Update == PUT, Delete == DELETE in case that is useful to you. Uploaded songs will be stored in "/path/to/your/songs".
So...assuming you have /path/to/your/songs/example-song.mp3
on your server then:
GET /songs/example-song.mp3
will return a partial content response and this should stream properly in most, if not all, players (including seeking forwards and backwards).
HTH
来源:https://stackoverflow.com/questions/49381568/spring-mvc-how-to-stream-mp3-file-from-controller