Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 04-11-2009
Member
 
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.
Reply With Quote
  #3  
Old 04-11-2009
Member
 
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.
Reply With Quote
  #4  
Old 04-11-2009
Member
 
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.
Reply With Quote
  #5  
Old 18-11-2009
Member
 
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) {
}
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 02:52 PM.