title: Saving mp3 file to client?
body: <p>I'm trying to save a mp3 file from the server to the client and play it after that. I am loading the playlist and after that trying to play a song(The song is supposed to be saved on the client PC and played after that). I am building the application with JavaFXpackage main;</p>
  
  <p>Controller class:</p>
  
  <p>public class AudioController {</p>
  
  <pre><code>@FXML
  private ResourceBundle resources;
  
  @FXML
  private URL location;
  
  @FXML
  private Button nextButton;
  
  @FXML
  private Button playButton;
  
  @FXML
  private ListView&lt;String&gt; playlist;
  
  @FXML
  private Button previousButton;
  
  @FXML
  private Button stopButton;
  
  @FXML
  void initialize() {
      final AudioClient audioClient = new AudioClient("127.0.0.1", 3000);
      audioClient.setUpConnection();
  
      ArrayList&lt;String&gt; songList = audioClient.getPlaylist();
      ObservableList&lt;String&gt; songs = FXCollections
              .observableArrayList(songList);
      playlist.setItems(songs);
  
  
  
      playButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
          @Override
          public void handle(ActionEvent event) {
              String songToPlay = playlist.getSelectionModel()
                      .getSelectedItem();
              if (songToPlay != null) {
                  try {
                      String linkToSong = audioClient.getSong(songToPlay);
                      Media song = new Media(linkToSong);
                      MediaPlayer mediaPlayer = new MediaPlayer(song);
                      mediaPlayer.play();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
              }
          }
      });
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Client class </p>
  
  <pre><code>public class AudioClient {
  protected BufferedReader socketReader;
  protected PrintWriter socketWriter;
  protected InputStream is;
  protected String hostIP;
  protected int hostPort;
  
  
  public AudioClient(String hostIP, int hostPort) {
      this.hostIP = hostIP;
      this.hostPort = hostPort;
  }
  
  public ArrayList&lt;String&gt; getPlaylist() {
  
      ArrayList&lt;String&gt; fileLines = new ArrayList&lt;String&gt;();
      try {
          socketWriter.println("Playlist");
          socketWriter.flush();
          String line = null;
          while ((line = socketReader.readLine()) != null) {
              fileLines.add(line);
          }
      } catch (IOException e) {
          System.out.println("There was a problem reading");
      }
      return fileLines;
  }
  
  public String getSong(String songName) throws IOException {
  
      socketWriter.println("Request " + songName);
      socketWriter.flush();
      String filePath = new String("D:\\" + songName + ".mp3");
      FileOutputStream fos = new FileOutputStream(new File(filePath));
      BufferedOutputStream bos = new BufferedOutputStream(fos);
      int count;
      byte[] buffer = new byte[4096];
      while ((count = is.read(buffer, 0, buffer.length)) != -1) {
          bos.write(buffer, 0, count);
      }
  
      return filePath;
  }
  
  public void setUpConnection() {
      try {
          Socket client = new Socket(hostIP, hostPort);
          socketReader = new BufferedReader(new InputStreamReader(
                  client.getInputStream()));
          InputStream is = client.getInputStream();
          socketWriter = new PrintWriter(client.getOutputStream());
      } catch (UnknownHostException e) {
          System.out.println("Error setting up socket connection: unknown host at "
                          + hostIP + ":" + hostPort);
      } catch (IOException e) {
          System.out.println("Error setting up socket connection: " + e);
      }
  }
  
  public void tearDownConnection() {
      try {
          socketWriter.close();
          socketReader.close();
      } catch (IOException e) {
          System.out.println("Error tearing down socket connection: " + e);
      }
  }
  </code></pre>
  
  <p>Class for handling connections to the server</p>
  
  <pre><code>public class ConnectionHandler implements Runnable {
  private Socket socketToHandle;
  
  public ConnectionHandler(Socket aSocketToHandle) {
      socketToHandle = aSocketToHandle;
  }
  
  @Override
  public void run() {
      try {
          PrintWriter streamWriter = new PrintWriter(
                  socketToHandle.getOutputStream());
          BufferedReader streamReader = new BufferedReader(
                  new InputStreamReader(socketToHandle.getInputStream()));
          String songToPlay = null;
          while ((songToPlay = streamReader.readLine()) != null) {
              if (songToPlay.equals("Playlist")) {
                  File songsTxt = new File(
                          "D:/Universitet i dr/JAVA/AudioPlayer/src/main/media/songsList.txt");
                  String song = null;
                  try {
                      FileReader reader = new FileReader(songsTxt);
                      BufferedReader songReader = new BufferedReader(reader);
                      while ((song = songReader.readLine()) != null) {
                          streamWriter.println(song);
                      }
                  } catch (FileNotFoundException e) {
                      System.out.println("File dosen't exist");
                  } catch (IOException e) {
                      System.out.println("Can't read from the file");
                  }
              } else if (songToPlay.startsWith("Request ")) {
                  songToPlay = songToPlay.replaceFirst("Request ", "");
  
                  File songPath = new File(
                          "D:/Universitet i dr/JAVA/AudioPlayer/src/main/media/"
                                  + songToPlay);
                  BufferedInputStream inStream = new BufferedInputStream(
                          new FileInputStream(songPath));
                  BufferedOutputStream outStream = new BufferedOutputStream(
                          socketToHandle.getOutputStream());
                  byte[] buffer = new byte[4096];
                  for (int read = inStream.read(buffer); read &gt;= 0; read = inStream.read(buffer)) {
                      outStream.write(buffer, 0, read);
                  }
              }
  
          }
      } catch (Exception e) {
          System.out.println("Error handling a client: " + e);
      }
  }
  </code></pre>