| | |
downloading to a certain directory
![]() |
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:
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:
java Syntax (Toggle Plain Text)
package downloaderPKG; import java.io.*; import java.net.*; public class FileDownload { public static void download(String address, String localFileName) { OutputStream out = null; URLConnection conn = null; InputStream in = null; try { URL url = new URL(address); out = new BufferedOutputStream( new FileOutputStream(localFileName)); conn = url.openConnection(); in = conn.getInputStream(); byte[] buffer = new byte[1024]; int numRead; long numWritten = 0; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); numWritten += numRead; } System.out.println(localFileName + "\t" + numWritten); } catch (Exception exception) { exception.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException ioe) { } } } public static void download(String address) { int lastSlashIndex = address.lastIndexOf('/'); if (lastSlashIndex >= 0 && lastSlashIndex < address.length() - 1) { download(address, address.substring(lastSlashIndex + 1)); } else { System.err.println("Could not figure out local file name for " + address); } } }
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
---------------------------------- Sir Winston Churchill
Okay so I modified
Here is my error:
new FileOutputStream("C:\\"+localFileName)); now here is my code: java Syntax (Toggle Plain Text)
package downloaderPKG; import java.io.*; import java.net.*; public class FileDownload { public static void download(String address, String localFileName) { OutputStream out = null; URLConnection conn = null; InputStream in = null; try { URL url = new URL(address); out = new BufferedOutputStream( new FileOutputStream("C:\\"+localFileName)); conn = url.openConnection(); in = conn.getInputStream(); byte[] buffer = new byte[1024]; int numRead; long numWritten = 0; while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); numWritten += numRead; } System.out.println(localFileName + "\t" + numWritten); } catch (Exception exception) { exception.printStackTrace(); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException ioe) { } } } public static void download(String address) { int lastSlashIndex = address.lastIndexOf('/'); if (lastSlashIndex >= 0 && lastSlashIndex < address.length() - 1) { download(address, address.substring(lastSlashIndex + 1)); } else { System.err.println("Could not figure out local file name for " + address); } } }
Here is my error:
Java Syntax (Toggle Plain Text)
java.io.FileNotFoundException: C:\application.jar (Access is denied) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at downloaderPKG.FileDownload.download(FileDownload.java:14) at MainClass.main(MainClass.java:11)
History will be kind to me for I intend to write it.
---------------------------------- Sir Winston Churchill
---------------------------------- Sir Winston Churchill
![]() |
Similar Threads
- Softwares worth downloading (Windows NT / 2000 / XP)
- Directory Download Script (Existing Scripts)
- Disabling downloading files (Web Browsers)
- trainers directory (Website Reviews)
- downloading class files (Java)
- Need help with directory displayer code (PHP)
- Downloading......Always Copying to Temp? (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: temperature
- Next Thread: console.nextChar(); ???
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





