This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes save file into directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "save file into directory" Watch "save file into directory" New topic
Author

save file into directory

ernie bert
Greenhorn

Joined: Apr 13, 2005
Posts: 4
hi all,
i need a method that downloads a file from the internet and saves it into a directory which has to be created if it doesn't exist. how can i do this? i figured out something like this:

public String getImage() {
String picurl = "www.server.net/sub/01.jpg";
URL neturl = new URL("http://"+picurl/");
InputStream in = neturl.openConnection().getInputStream();
String localurl = "C:/Cache/"+picurl;
File picture = new File(localurl);
OutputStream out = new FileOutputStream(localurl);
byte[] buffer = new byte[1024];
for (int n;(n = in.read(buffer)) != -1; out.write(buffer, 0, n));
in.close();
out.close();
return result; //local path of file = "C:/Cache/www.server.net/sub/01.jpg"

but that doesn't seem to be the solution... how do i save the file into a (new) directory and return the path of the file as a String? can anybody help?
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
you will find all the information you need here.
Next time you post code use the ubb code tag, there is a button underneath the text input area to make this easy to do. The code tags mantain your indentations making your code easier to read.
[ April 13, 2005: Message edited by: Nigel Browne ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: save file into directory