Java tutorial
package gethanselminutes; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; /** * Download Hanselminutes podcasts. Uses apache commons io FileUtils.copyURLToFile to download the mp3 files. * * @author skorkmaz, June 2016 * @license Public Domain */ public class GetHanselminutes extends javax.swing.JFrame { private static final String BASE_URL = "https://s3.amazonaws.com/hanselminutes/hanselminutes_"; public GetHanselminutes() { initComponents(); jlInfo.setText(""); } /** * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The * content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); spStart = new javax.swing.JSpinner(); spEnd = new javax.swing.JSpinner(); jbDownload = new javax.swing.JButton(); jlInfo = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("start:"); jLabel2.setText("end:"); spStart.setValue(358); spEnd.setValue(355); jbDownload.setText("Download"); jbDownload.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jbDownloadActionPerformed(evt); } }); jlInfo.setText("jLabel3"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(spStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(spEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jbDownload).addComponent(jlInfo)) .addContainerGap(116, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(16, 16, 16) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(spStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(spEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jbDownload) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jlInfo) .addContainerGap(17, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jbDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDownloadActionPerformed long startTime = System.nanoTime(); jbDownload.setEnabled(false); int iStart = (Integer) spStart.getValue(); int iEnd = (Integer) spEnd.getValue(); int nEpisodes = iStart - iEnd + 1; jlInfo.setText("Downloading " + nEpisodes + " episodes..."); Thread t = new Thread(() -> { try { for (int i = iStart; i >= iEnd; i--) { FileUtils.copyURLToFile(new URL(BASE_URL + "0" + i + ".mp3"), new File("Hanselminutes 0" + i + ".mp3")); jlInfo.setText("Finished " + (nEpisodes - (i - iEnd)) + " of " + nEpisodes); } } catch (MalformedURLException ex) { Logger.getLogger(GetHanselminutes.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GetHanselminutes.class.getName()).log(Level.SEVERE, null, ex); } finally { jbDownload.setEnabled(true); long endTime = System.nanoTime(); long dt_s = (long) ((endTime - startTime) / 1e9); jlInfo.setText(jlInfo.getText() + ". Time [s] = " + dt_s); } }); t.start(); }//GEN-LAST:event_jbDownloadActionPerformed public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(GetHanselminutes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(GetHanselminutes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(GetHanselminutes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GetHanselminutes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GetHanselminutes().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JButton jbDownload; private javax.swing.JLabel jlInfo; private javax.swing.JSpinner spEnd; private javax.swing.JSpinner spStart; // End of variables declaration//GEN-END:variables }