Results 1 to 9 of 9
- 11-19-2011, 04:32 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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:
dlgui 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"); } }
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);} } }
- 11-19-2011, 06:04 PM #2
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.
- 11-19-2011, 06:08 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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.
- 11-19-2011, 06:14 PM #4
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.
- 11-19-2011, 06:18 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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
- 11-19-2011, 06:31 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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");
- 11-19-2011, 06:42 PM #7
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.
- 11-19-2011, 06:43 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Re: File Downloader: getting progress out the download
figured it out.
- 11-19-2011, 06:45 PM #9
Similar Threads
-
How to set the value for Progress Bar in another Java file not in UI class?
By raja e in forum SWT / JFaceReplies: 1Last Post: 01-25-2011, 05:38 AM -
monitor the progress of sending a file to client
By adumi in forum Advanced JavaReplies: 9Last Post: 04-23-2010, 09:55 PM -
How to show or open a file download or file save dialog box
By java_bond in forum New To JavaReplies: 0Last Post: 03-05-2010, 04:21 AM -
JFileDownload 2.5 - Java file downloader
By jfileupload in forum Java SoftwareReplies: 1Last Post: 11-12-2009, 07:55 AM -
Download file from URL?
By makpandian in forum NetworkingReplies: 0Last Post: 03-10-2009, 12:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks