September 04, 2010, 02:55:26 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Fetching files over a network  (Read 512 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« on: June 06, 2009, 11:30:17 AM »

Code
GeSHi (java):
  1. package networkanddatabase;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.Iterator;
  11. import java.util.List;
  12.  
  13. import org.apache.ivy.util.url.ApacheURLLister;
  14.  
  15. public class FetchFilesFromHttpURL {
  16. public static void main(String[] args) {
  17. URL url;
  18. try {
  19. url = new URL("http://engineeringserver.com/fetchfiles/");
  20. File destFolder = new File("c:\\test");
  21. ApacheURLLister lister = new ApacheURLLister();
  22. // this list of URLs objects
  23. List files = lister.listAll(url);
  24. System.out.println("list file is complete.."+files);
  25. for (Iterator iter = files.iterator(); iter.hasNext();) {
  26. URL fileUrl = (URL) iter.next();
  27. httpFileDownload(fileUrl, destFolder);
  28. }
  29. System.out.println("download is complete..");
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33.  
  34. }
  35.  
  36. public static void httpFileDownload(URL url, File destFolder) throws Exception {
  37. File destination = new File(destFolder, url.getFile());
  38. destination.getParentFile().mkdirs();
  39. BufferedInputStream bis = null;
  40. try {
  41. URLConnection urlc = url.openConnection();
  42.  
  43. bis = new BufferedInputStream(urlc.getInputStream());
  44. bos = new BufferedOutputStream(new FileOutputStream(destination.getPath()));
  45.  
  46. int i;
  47. while ((i = bis.read()) != -1) {
  48. bos.write(i);
  49. }
  50. } finally {
  51. if (bis != null)
  52. try {
  53. bis.close();
  54. } catch (IOException ioe) {
  55. ioe.printStackTrace();
  56. }
  57. if (bos != null)
  58. try {
  59. bos.close();
  60. } catch (IOException ioe) {
  61. ioe.printStackTrace();
  62. }
  63. }
  64. }
  65. }
Created by GeSHI 1.0.7.20
« Last Edit: August 26, 2009, 09:13:52 AM by _ikram » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
« on: June 06, 2009, 11:30:17 AM »

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.234 seconds with 32 queries.

Google visited last this page Yesterday at 04:08:49 AM