permaculture playing cards*
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
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 ]
 
 
subject: save file into directory
 
Similar Threads
how to zip multiple files and directory into one output
How to make sense of my binary data
how to store image object
trouble retrieving html of all web sites
Not able to put some files inside Zip file