Java tutorial
/** * Copyright 2017 tesshu.com (webmaster@tesshu.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.tesshu.subsonic.client.sample4_music_andmovie; import com.tesshu.subsonic.client.controller.Search2Controller; import com.tesshu.subsonic.client.controller.StreamController; import com.tesshu.subsonic.client.model.Child; import com.tesshu.subsonic.client.model.SearchResult2; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.client.RestTemplate; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import javax.annotation.Resource; /** * Acquire the stream using format conversion of the server and store the media * locally. */ @SpringBootApplication @ComponentScan(basePackages = { "com.tesshu.subsonic.client" }) public class StreamDownloadApplication { private static String tmpPath = "target/tmp"; private String format = "mp3"; @Resource private Search2Controller search2; @Resource private StreamController streamController; public static void main(String[] args) { SpringApplication.run(StreamDownloadApplication.class); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } @Bean protected CommandLineRunner run(RestTemplate restTemplate) throws Exception { return args -> { SearchResult2 result2 = search2.get("e", null, null, null, null, 1, null, null); List<Child> songs = result2.getSongs(); File tmpDirectory = new File(tmpPath); tmpDirectory.mkdir(); int maxBitRate = 256; for (Child song : songs) { streamController.stream(song, maxBitRate, format, null, null, null, null, (subject, inputStream, contentLength) -> { File dir = new File( tmpPath + "/" + song.getPath().replaceAll("([^/]+?)?$", StringUtils.EMPTY)); dir.mkdirs(); File file = new File(tmpPath + "/" + song.getPath().replaceAll("([^.]+?)?$", StringUtils.EMPTY) + format); try { FileOutputStream fos = new FileOutputStream(file); BufferedInputStream reader = new BufferedInputStream(inputStream); byte buf[] = new byte[256]; int len; while ((len = reader.read(buf)) != -1) { fos.write(buf, 0, len); } fos.flush(); fos.close(); reader.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } }); } }; } }