Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save file into directory

 
ernie bert
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic