Results 1 to 5 of 5

Thread: Loading java files via HTTP

  1. #1
    Join Date
    Jul 2009
    Posts
    127

    Loading java files via HTTP

    Hi
    I have created a small application and I need to load some files via HTTP protocol. I know this can be done in java, but I do no know the exact class and package to use. The files which I am trying to load are on my local computer (not an URL). I read the java API documentation, but was not successful. Can any one help.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Loading java files via HTTP

    This is how you can do it. Take a look on the example below.
    Code:
    try
    {
       java.net.URL url = new java.net.URL("http://www.mywebsite.com/someplace/test.txt");
    
       InputStreamReader inStream = new InputStreamReader(url.openStream());
       BufferedReader in = new BufferedReader(inStream);
    
       String line = in.readLine();
       while(line!=null)
       {
          System.out.println(line);
          line = in.readLine();
       }
       inStream.close();
    }
    catch(Exception e)
    {
       e.printStackTrace();
    }
    I think this is what you were asking for. If not, please do post back.

  3. #3
    Join Date
    Jul 2009
    Posts
    127

    Re: Loading java files via HTTP

    Thank you Praetor. This is exactly what I was looking for. I have questions in my mind, first will it work if the URL mentioned is on the local PC like "c:\test\ex.txt" and second will it be HTTP loading the files. Thanks for the help.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Loading java files via HTTP

    The following line does this through HTTP
    Code:
    java.net.URL url = new java.net.URL("file:///c:/test/ex.txt");
    Any more queries feel free to post.

  5. #5
    Join Date
    Nov 2009
    Posts
    1

    blush Re: Loading java files via HTTP

    Note that solution is good for text files. This will work with binary files.

    try {
    URL url = new URL(urlParam);
    InputStream is = url.openStream();
    fos = new FileOutputStream(file);

    byte[] buffer = new byte[1000];

    int count = 0;
    while((count = is.read(buffer)) > -1) {
    fos.write(buffer, 0, count);
    }
    }
    catch (IOException e) {
    System.out.println("Error retrieving file: " + e.getMessage));
    }
    finally {
    if (fos != null) {
    try {
    fos.close();
    }
    catch (IOException e) {
    }
    }
    }

Similar Threads

  1. BlackBerry Playbook: Java language error Http status 500
    By Zelman in forum Portable Devices
    Replies: 5
    Last Post: 21-05-2011, 10:34 AM
  2. Send Http Request In Java
    By ramsun in forum Software Development
    Replies: 5
    Last Post: 26-02-2010, 09:55 PM
  3. How to create a Loading bar in Java?
    By Taylor D in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 09:53 AM
  4. Frequent error while loading page 'HTTP 400 Bad Request'
    By CossFire in forum Technology & Internet
    Replies: 3
    Last Post: 03-06-2009, 11:17 AM
  5. Loading Java Applet Failed
    By Sanith in forum Technology & Internet
    Replies: 2
    Last Post: 25-03-2009, 10:03 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,816,076.51424 seconds with 16 queries