downloading to a certain directory

Reply

Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

downloading to a certain directory

 
0
  #1
Feb 22nd, 2009
Hey guys,
I wanna download a file to a certain directory on the computer. I have code that downloads to the same place where it's launched but I want it to download the files do another directory.

(This isnt my code I found it on a tutorial)

Here is the code:
  1. package downloaderPKG;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. public class FileDownload {
  7. public static void download(String address, String localFileName) {
  8. OutputStream out = null;
  9. URLConnection conn = null;
  10. InputStream in = null;
  11. try {
  12. URL url = new URL(address);
  13. out = new BufferedOutputStream(
  14. new FileOutputStream(localFileName));
  15. conn = url.openConnection();
  16. in = conn.getInputStream();
  17. byte[] buffer = new byte[1024];
  18. int numRead;
  19. long numWritten = 0;
  20. while ((numRead = in.read(buffer)) != -1) {
  21. out.write(buffer, 0, numRead);
  22. numWritten += numRead;
  23. }
  24. System.out.println(localFileName + "\t" + numWritten);
  25. } catch (Exception exception) {
  26. exception.printStackTrace();
  27. } finally {
  28. try {
  29. if (in != null) {
  30. in.close();
  31. }
  32. if (out != null) {
  33. out.close();
  34. }
  35. } catch (IOException ioe) {
  36. }
  37. }
  38. }
  39.  
  40. public static void download(String address) {
  41. int lastSlashIndex = address.lastIndexOf('/');
  42. if (lastSlashIndex >= 0 &&
  43. lastSlashIndex < address.length() - 1) {
  44. download(address, address.substring(lastSlashIndex + 1));
  45. } else {
  46. System.err.println("Could not figure out local file name for " +
  47. address);
  48. }
  49. }
  50. }
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 30
Reputation: punitdam is an unknown quantity at this point 
Solved Threads: 3
punitdam's Avatar
punitdam punitdam is offline Offline
Light Poster

Re: downloading to a certain directory

 
0
  #2
Feb 22nd, 2009
Just specify the entire path where you want to store file as localFileName.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 128
Reputation: PhiberOptik is an unknown quantity at this point 
Solved Threads: 4
PhiberOptik's Avatar
PhiberOptik PhiberOptik is offline Offline
Junior Poster

Re: downloading to a certain directory

 
0
  #3
Feb 22nd, 2009
Okay so I modified new FileOutputStream("C:\\"+localFileName)); now here is my code:
  1. package downloaderPKG;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. public class FileDownload {
  7. public static void download(String address, String localFileName) {
  8. OutputStream out = null;
  9. URLConnection conn = null;
  10. InputStream in = null;
  11. try {
  12. URL url = new URL(address);
  13. out = new BufferedOutputStream(
  14. new FileOutputStream("C:\\"+localFileName));
  15. conn = url.openConnection();
  16. in = conn.getInputStream();
  17. byte[] buffer = new byte[1024];
  18. int numRead;
  19. long numWritten = 0;
  20. while ((numRead = in.read(buffer)) != -1) {
  21. out.write(buffer, 0, numRead);
  22. numWritten += numRead;
  23. }
  24. System.out.println(localFileName + "\t" + numWritten);
  25. } catch (Exception exception) {
  26. exception.printStackTrace();
  27. } finally {
  28. try {
  29. if (in != null) {
  30. in.close();
  31. }
  32. if (out != null) {
  33. out.close();
  34. }
  35. } catch (IOException ioe) {
  36. }
  37. }
  38. }
  39.  
  40. public static void download(String address) {
  41. int lastSlashIndex = address.lastIndexOf('/');
  42. if (lastSlashIndex >= 0 &&
  43. lastSlashIndex < address.length() - 1) {
  44. download(address, address.substring(lastSlashIndex + 1));
  45. } else {
  46. System.err.println("Could not figure out local file name for " +
  47. address);
  48. }
  49. }
  50. }

Here is my error:
  1. java.io.FileNotFoundException: C:\application.jar (Access is denied)
  2. at java.io.FileOutputStream.open(Native Method)
  3. at java.io.FileOutputStream.<init>(Unknown Source)
  4. at java.io.FileOutputStream.<init>(Unknown Source)
  5. at downloaderPKG.FileDownload.download(FileDownload.java:14)
  6. at MainClass.main(MainClass.java:11)
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC