Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Downloading file from website and saving it locally.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Downloading file from website and saving it locally.

    Hello friends...

    I'm currently struggling with this code:

    import java.awt.FlowLayout;
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import javax.swing.JFrame;
    import javax.swing.JProgressBar;
     
    public class trying extends JFrame
    {
    	public static final void main(String[] args) throws Exception, FileNotFoundException
    	{
    		String site="http://example.com/c/example/aps/example.exe";
    		String filename="example.exe";
    		JFrame frm=new JFrame();
    		JProgressBar current = new JProgressBar(0, 100);
    		current.setSize(50,50);
    		current.setValue(43);
    		current.setStringPainted(true);
    		frm.add(current);
    		frm.setVisible(true);
    		frm.setLayout(new FlowLayout());
    		frm.setSize(400, 100);
    		frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		try {
    			URL url=new URL(site);
    			HttpURLConnection connection =
    					(HttpURLConnection) url.openConnection();
    			int filesize = connection.getContentLength();
    			float totalDataRead=0;
    			java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
    			java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
    			java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
    			byte[] data = new byte[1024];
    			int i=0;
    			while((i=in.read(data,0,1024))>=0)
    			{
    				totalDataRead=totalDataRead+i;
    				bout.write(data,0,i);
    				float Percent=(totalDataRead*100)/filesize;
    				current.setValue((int)Percent);
    			}  
    			bout.close();
    			in.close();
    		}
    		catch(Exception e)
    		{
    			javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
    					null,e.getMessage(), "Error",
    					javax.swing.JOptionPane.DEFAULT_OPTION);
    		}
    	}
    }

    This program will (is supposed to) download an executable or any file wherever is located, if i place it in the desktop, works fine, documents, still working.

    But this program to work for we, it needs to be in this path: "C:\Windows\System32\drivers\etc"

    When I run it, it says "access denied" and I'm the administrator and every permission is fine; is there any way that java will just ignore any permissions and just put the file where I want it to be?

    Thanks and I hope you guys can help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Downloading file from website and saving it locally.

    Looks like your OS is trying to protect your PC from programs that try to write to locations that are secured.
    I don't think a program should be able to override the OS. You probably need to tell the OS that is is OK for this program to do the update.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    JosPhantasmE (March 28th, 2013)

  4. #3
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Downloading file from website and saving it locally.

    Can you tell me, if you have an idea on how can I tell the OS that it is okay to update?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Downloading file from website and saving it locally.

    Try asking that question on a forum for your OS?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Downloading file from website and saving it locally.

    Cant find any information... Is there any other ideas? How can I modify this code to save to the local temp folder...?

    --- Update ---

    I'm working on windows...

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Downloading file from website and saving it locally.

    I'm not sure how to get a path to "the local temp folder". Is there a system path to it?
    Print out the system properties to see if java has a path to it:
          Properties props = System.getProperties();
         props.list(System.out);
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Downloading file from website and saving it locally.

    Quote Originally Posted by Norm View Post
    I'm not sure how to get a path to "the local temp folder". Is there a system path to it?
    Print out the system properties to see if java has a path to it:
          Properties props = System.getProperties();
         props.list(System.out);
    String default_tmp = System.getProperty("java.io.tmpdir");
    System.out.println(default_tmp);

    This should result in something similar to:
    C:\Users\Jos\AppData\Local\Temp\

Similar Threads

  1. multiple file uploading and downloading
    By priti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 27th, 2012, 12:34 PM
  2. Problem downloading file from internet
    By loicus in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: November 21st, 2011, 01:29 AM
  3. Downloading the file in Lotus Symphony spreadsheet format (ods)
    By chinnu in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 13th, 2011, 03:47 AM
  4. saving xml file
    By LOL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 18th, 2010, 06:45 AM
  5. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 12:56 PM