Results 1 to 9 of 9
  1. #1
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default File Downloader: getting progress out the download

    I have this code to download a specified file at the specified path and name, but i cannot get it to display the percent already downloaded, or the file size of the download, or the amount i have already downloaded. These three operations are in the edit() method, and i tried to call it in the downloading loop, but then it will not download the file. I tried threads, but i don't think i used them right. Any help is appreciated :)
    SampleDownload class:
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SampleDownload {
    
    	public static double percentDled;
    	public static int count = 0;
    	public static int dlSize;
    	public static dlgui gui;
    
    	public static void main(String args[]) {
    		gui =  new dlgui();
    		gui.showIt();
    		while(!dlgui.b) {
    			try {
    				Thread.sleep(100);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	public static void download(String s, String path) throws IOException {
    		URL dlURL = new URL(s);
    		HttpURLConnection con = (HttpURLConnection) dlURL.openConnection();
    		int i = con.getContentLength();
    		double dlSize =  i * 1024;
    		BufferedInputStream in = new java.io.BufferedInputStream(dlURL.openStream());
    		FileOutputStream fos = new java.io.FileOutputStream(path);
    		BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
    		byte data[] = new byte[1024];
    		while((count = in.read(data,0,1024)) != -1) {
    			bout.write(data,0,count);
    		}
    
    
    
    		bout.close();
    		in.close();
    	}
    
    	public static void edit() {
    		gui.label_2.setText("Downloaded:" + percentDled + "%");
    		gui.label_3.setText("Downloaded:"+ count * 1024 + " KB out of "+ dlSize + " KB");
    	}
    }
    dlgui class:
    Java Code:
    class dlgui extends JFrame {
        JTextField textfield_1;
        JLabel label_1;
        JLabel label_2;
        JLabel label_3;
        JLabel label_4;
        JTextField textfield_2;
        JButton button_1;
        public String url1;
        public static boolean b;
    
        public dlgui() {
            dlguiLayout customLayout = new dlguiLayout();
    
            getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
            getContentPane().setLayout(customLayout);
    
            textfield_1 = new JTextField("");
            getContentPane().add(textfield_1);
    
            label_1 = new JLabel("URL:");
            getContentPane().add(label_1);
    
            label_2 = new JLabel("Downloaded:" + SampleDownload.percentDled + "%");
            getContentPane().add(label_2);
    
            label_3 = new JLabel("Downloaded:"+ SampleDownload.count * 1024 + " KB out of "+ SampleDownload.dlSize + " KB");
            getContentPane().add(label_3);
    
            label_4 = new JLabel("Save To:");
            getContentPane().add(label_4);
    
            textfield_2 = new JTextField("C:/Users/AJ/");
            getContentPane().add(textfield_2);
    
            button_1 = new JButton("Start!");
            button_1.addActionListener(new ActionListener () {
            	public void actionPerformed(ActionEvent e) {
            		URL ghf = null;
            		b = true;
            		String path = textfield_2.getText();
            		url1 = textfield_1.getText();
            		try {
    					ghf = new URL(url1);
    				} catch (MalformedURLException e2) {
    					e2.printStackTrace();
    				}
            		try {
    					SampleDownload.download(url1,path);
    				} catch (IOException e1) {
    					e1.printStackTrace();
    				}
            	}
            });
            getContentPane().add(button_1);
    
            setSize(getPreferredSize());
    
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    
        public void showIt() {
            dlgui window = new dlgui();
    
            window.setTitle("dlgui");
            window.pack();
            window.setVisible(true);
        }
    }
    
    class dlguiLayout implements LayoutManager {
    
        public dlguiLayout() {
        }
    
        public void addLayoutComponent(String name, Component comp) {
        }
    
        public void removeLayoutComponent(Component comp) {
        }
    
        public Dimension preferredLayoutSize(Container parent) {
            Dimension dim = new Dimension(0, 0);
    
            Insets insets = parent.getInsets();
            dim.width = 283 + insets.left + insets.right;
            dim.height = 174 + insets.top + insets.bottom;
    
            return dim;
        }
    
        public Dimension minimumLayoutSize(Container parent) {
            Dimension dim = new Dimension(0, 0);
            return dim;
        }
    
        public void layoutContainer(Container parent) {
            Insets insets = parent.getInsets();
    
            Component c;
            c = parent.getComponent(0);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+40,256,24);}
            c = parent.getComponent(1);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+8,80,24);}
            c = parent.getComponent(2);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+104,120,24);}
            c = parent.getComponent(3);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,184,24);}
            c = parent.getComponent(4);
            if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+72,72,24);}
            c = parent.getComponent(5);
            if (c.isVisible()) {c.setBounds(insets.left+88,insets.top+72,176,24);}
            c = parent.getComponent(6);
            if (c.isVisible()) {c.setBounds(insets.left+200,insets.top+136,72,24);}
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,339
    Rep Power
    25

    Default Re: File Downloader: getting progress out the download

    Is your problem:
    gathering the statistics
    or displaying them?

    Where do you call the edit() method?

    You need to add lots of debug printlns to show what the code is doing.
    Last edited by Norm; 11-19-2011 at 06:13 PM.

  3. #3
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: File Downloader: getting progress out the download

    My problem is displaying them. Sorry, I called the edit() before the while loop, and I tried it in the while loop and both times it did not work. It would complete the download and freeze, not updating the statistics either of the times.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,339
    Rep Power
    25

    Default Re: File Downloader: getting progress out the download

    You need to add lots of printlns to show where the code is executing and what the values are as the code executes.

  5. #5
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: File Downloader: getting progress out the download

    i know that the code is not even executing the edit method, because i changed it to have nothing by default and when the edit method is called, it should show "agh" in each JLabel, and it did not change them. I will add printlns

  6. #6
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: File Downloader: getting progress out the download

    i added printlns in edit() download() and the actionlistener for the start button
    they all displayed what they were supposed to, so it's a matter of the two lines:
    gui.label_2.setText("Downloaded:" + percentDled + "%");
    gui.label_3.setText("Downloaded:"+ count * 1024 + " KB out of "+ dlSize + " KB");

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,339
    Rep Power
    25

    Default Re: File Downloader: getting progress out the download

    One problem I am having with your code is that is has too many static variables and methods.
    There should only be one: main.

  8. #8
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: File Downloader: getting progress out the download

    figured it out.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,339
    Rep Power
    25

    Default Re: File Downloader: getting progress out the download

    Get rid of all the static.

Similar Threads

  1. Replies: 1
    Last Post: 01-25-2011, 05:38 AM
  2. monitor the progress of sending a file to client
    By adumi in forum Advanced Java
    Replies: 9
    Last Post: 04-23-2010, 09:55 PM
  3. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  4. JFileDownload 2.5 - Java file downloader
    By jfileupload in forum Java Software
    Replies: 1
    Last Post: 11-12-2009, 07:55 AM
  5. Download file from URL?
    By makpandian in forum Networking
    Replies: 0
    Last Post: 03-10-2009, 12:31 PM

Tags for this Thread

Posting Permissions

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