• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a File with URI

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am trying to create a file with a URI.Following is a piece of code that I am using:

URI uri = new URI("http://146.45.2.44/downloads/materials/bcc.txt");
File test = new File(uri);

When I try to create a new File using the 2nd line,I get an exception which says "URI scheme is not "file".

I intend to use this URI to locate files using the URI and then stream them using my Servlet. The code works fine when the files are located in the WEB-INF folder.But the issue is when the files are located in a totally different area in the UNIX box.In such a situation,I need to use the URI.Is there a better way to do this?

Please advise.

Your help is appreciated.

Thanks
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
File only represents files accessible through the local file system. In other words, files on your hard drives, CDs, DVDs, USB devices and on the network through Windows Sharing. That list does not include HTTP.

If you need to access "files" on a remote machine, you can use URL and URLConnection. It isn't a file though, but you can at least read its contents.

If they are files on the same UNIX box where your servlet container is running, you can just use File. You just have to specify the absolute file path, or the relative file path to the servlet context. For instance:
 
Rumi Harry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Thanks for your quick response Rob.I tried using what you mentioned.But it doesn't work.Following is my piece of code:

String serverpath = "/downloads/materials/";
String fileName = read.txt;
File f = new File(serverpath+fileName);

The UNIX box on which the servlet as well as the files reside is 149.42.3.42.

I expect the servlet to locate files under http://149.42.3.42/downloads/materials.Instead I think it looks for files in the /downloads/materials under WEB-INF which is where the servlet is deployed.For some reason,the path http://149.42.3.42/downloads/materials is not being used.

I also tried using String serverpath = "http://149.41.1.45/downloads/materials/";

That did not work too.When I said the following:

File test = new File("http://149.41.1.45/downloads/materials/");
and then did
System.out.println(test.toString()) I got tthe following-http:/149.41.1.45/downloads/materials/".One slash '/' was missing.




Please advise.

Thanks.
[ December 18, 2008: Message edited by: Rumi Harry ]
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Rob said, you can't get a File object from an HTTP URI. Why do you think you need a File object, anyway? It's possible to get an InputStream from an HTTP URI, for example.
 
Rumi Harry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you reply Paul.But Rob mentions in his reply that in a situation where you have a servlet deployed in a UNIX box,then you could use the File object to locate files.I was trying to do exactly that.

Thanks
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rumi Harry:
Thanks for you reply Paul.But Rob mentions in his reply that in a situation where you have a servlet deployed in a UNIX box,then you could use the File object to locate files.I was trying to do exactly that.

Yes, but first he said you can't use an HTTP URI to do it. And you don't use a File object to "locate" a file. It's the other way around. You need to know the location of the file before you can create a File object.

So just provide the full path to the file.
 
Rumi Harry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul,
Thanks for your reply.Heres what I tried to do:

String serverpath = "/downloads/materials/";
String fileName = read.txt;
File f = new File(serverpath+fileName);

So I expect the servlet to read the file read.txt under "http://server_name/downloads/materials/read.txt".But is happening is the servlet is trying to read the file under http://server_name/WEB-INF/downloads/materials.WEB-INF is where the servlet is deployed.

Thanks for your patience.

Regards
RumiHarry
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why on earth would you expect that?
 
Rumi Harry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I probably don't know enough.But if I need a file ex read.txt which is under
/server_name/downloads/materials to be read,how do I create the file?

Is it going to be :

File f = new File("/server_name/downloads/materials/read.txt").



Thanks once again

Regards
RumiHarry
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that's the full path of the file, then yes. Otherwise, no, you need to provide the absolute path. Bear in mind that the fact that you have some kind of server software running on the machine has nothing at all to do with the name of the file. The fact that the software is being accessed by HTTP requests has nothing to do with it either. That's all irrelevant. You just need to provide the path.
 
Rumi Harry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it working.Thanks for all your help.I really appreciate it.

Thanks
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xxxxx
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pranjal,
Please start a new thread for your question.
http://faq.javaranch.com/java/UseOneThreadPerQuestion
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic