• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

save file into directory

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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));
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?
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic