Results 1 to 10 of 10
  1. #1
    peteosmith is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default Copying an mp3 from url to local file within swingworker

    Please see below code snippet.

    It does work when copying a .GIF and .PHP, but when I try to copy an MP3 it continues for a while and then stops (does not complete the download), I am guessing that MP3 files do not have carriage returns in them (not sure if this is true or relevant) , any help would be good!

    Process is not called yet, but I am not updating any swing items.

    THanks
    Java Code:
        private static class numberBytes {
    
            private final int value;
    
            numberBytes(int inValue) {
                this.value = inValue;
            }
        }
    
        private class downloadAlbum extends SwingWorker<Void, numberBytes> {
    
            @Override
            protected Void doInBackground() throws InterruptedException {
                           
                URLConnection conn = null;
                URL url = null;
                InputStream reader = null;
                HttpURLConnection uc = null;
                BufferedInputStream in = null;
                
                
                try {
                  
                   url = new URL("http://www.peteosmith.co.uk/chrome_logo.gif");
                    
                } catch (MalformedURLException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                try {
                    uc =(HttpURLConnection) url.openConnection();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                try {
                    in = new BufferedInputStream(uc.getInputStream());
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
    
                
    
    
                FileOutputStream writer;
                writer = null;
                
                try {
                    conn = url.openConnection();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                int fileLength = conn.getContentLength();
                progressBar.setMaximum(fileLength);
    
                try {
                    writer = new FileOutputStream("c:/Users/Pete Smith/test.gif");
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                           
                System.out.println("Reading file");
                System.out.println("File Size :"+fileLength);
                int r;
                            
                try {
                    while ( !isCancelled() && (r=in.read())!=-1){                    
                        System.out.println("r="+r);
                        writer.write(r);
                    }
    
                } catch (IOException ex) {
                   Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("out of loop");
    
                try {
                    writer.close();
                } catch (IOException ex) {
                    Logger.getLogger(albumFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
    
    
                   
                System.out.println("DONE!");
                statusLabel.setText("Download Complete!");
    
                return null;
            }
    
            @Override
            protected void process(List<numberBytes> currentBuffer) {
                int progValue;
                
             numberBytes numberY = currentBuffer.get(currentBuffer.size() -1); 
              System.out.println("Progress bar value"+numberY.value);
     
                progValue = numberY.value;
         
            }
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you do your download OK without any Swing code? Also, would you be better off buffering what you write? Also, you never replied to comments I made in your last thread before starting a new thread. What's with that?

  3. #3
    peteosmith is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    Sorry for not replying!

    I am totally new to forums and do not know the etiquette for thread use!
    The above code does work, its only when I try to do it with an MP3?

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by peteosmith View Post
    I am totally new to forums and do not know the etiquette for thread use!
    Most folks like to know that posters have read and considered their replies, simple as that.

    The above code does work, its only when I try to do it with an MP3?
    That's what I mean: can you download an MP3 with simple non-Swing/GUI Java code? Let's get Swing out of this equation.

  5. #5
    peteosmith is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    I have created the following program and the results are the same.
    Thanks again for your help!

    Java Code:
     package downloadtest;
    
    import java.io.BufferedInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Pete Smith
     */
    public class DownloadTest {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            URLConnection conn = null;
            URL url = null;
            InputStream reader = null;
            HttpURLConnection uc = null;
            BufferedInputStream in = null;
            try {
               
                url = new URL("http://www.peteosmith.co.uk/chrome_logo.gif");
            } catch (MalformedURLException ex) {
                Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                uc = (HttpURLConnection) url.openConnection();
            } catch (IOException ex) {
                Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                in = new BufferedInputStream(uc.getInputStream());
            } catch (IOException ex) {
                Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
    
    
            FileOutputStream writer;
            writer = null;
            try {
                conn = url.openConnection();
            } catch (IOException ex) {
                Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
            int fileLength = conn.getContentLength();
            try {
                writer = new FileOutputStream("c:/Users/Pete Smith/test.gif");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
    
            System.out.println("Reading file");
            System.out.println("File Size :" + fileLength);
            int r;
            try {
                while ((r = in.read()) != -1) {
                    System.out.println("r=" + r);
                    writer.write(r);
                }
            } catch (IOException ex) {
                Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
    
    
            System.out.println("out of loop");
            try {
                writer.close();
            } catch (IOException ex) {
                Logger.getLogger(DownloadTest.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            System.out.println("DONE!");
    
    
        }
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,418
    Rep Power
    25

    Default

    How many bytes does it read and print to the screen before ending?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I may not be the best person here to help you download files as I don't do this type of coding, but what if you use a byte[] as a buffer? Something like:

    Java Code:
        URLConnection conn = new URL("http://....").openConnection(); // fill in the correct http address
        InputStream is = conn.getInputStream();
    
        OutputStream outstream = new FileOutputStream(new File(....)); // fill in correct file name.
        byte[] buffer = new byte[4096];
        int len;
        while ((len = is.read(buffer)) > 0) {
            outstream.write(buffer, 0, len);
        }
        outstream.close(); // in a finally block of course

  8. #8
    peteosmith is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    I shall try using a byte[] as shown above.

    Thanks

  9. #9
    peteosmith is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    It works!!!!!!

    I shall try to plug it into the swing!

    GREAT!!!!!

  10. #10
    peteosmith is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default

    It only works within the swingworker - thanks again for your help!

Similar Threads

  1. add to a zip file without copying it.
    By yurabita in forum New To Java
    Replies: 5
    Last Post: 08-21-2010, 03:16 AM
  2. Copying a file (.docx) from computers
    By chyrl in forum Networking
    Replies: 3
    Last Post: 07-02-2010, 02:02 PM
  3. problems copying a file
    By atom86 in forum Advanced Java
    Replies: 16
    Last Post: 10-01-2009, 02:40 PM
  4. Creating Folder in JAVA and Copying File
    By fnoman in forum New To Java
    Replies: 8
    Last Post: 10-18-2008, 07:33 AM
  5. Replies: 13
    Last Post: 01-09-2008, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •