1

In my web application (spring REST ) I have an API to upload the file to the server.I have another API which retrieve the file url to the client.

file = new File(fileName);
 URL url = file.toURI().toURL();
 InetAddress  ip = InetAddress.getLocalHost();
 String urls="file://"+ip+url.toString();

As a result I am getting file://192.168.3.37/D:/Anoop/pic/2unvvhlacq5fh09tokr7i25cvj.jpg as the url. This works fine locally , When the application is hosted in a server url shows file not found. Please advice.

Anoop M Nair
  • 1,057
  • 1
  • 13
  • 31
  • file:// urls are intented for local files only. That is a client explicitly says that the file is on a computer where this client is running now. Also `192.168...` is an IP-range reserved for "hidden" private networks and unlikely accessible from an outside – user3159253 Sep 03 '15 at 07:04
  • Okay.Any suggestion/solution? – Anoop M Nair Sep 03 '15 at 07:07
  • `file://` denotes a local file system locator. If its hosted on the server, the server will think of its own local file system. It will try to look for directory `192.168.3.37` which contains folder `D:` which contains folter `Anoop`, so on and so fort. If the directory is not there, then file not found indeed. – Ferdinand Neman Sep 03 '15 at 07:08
  • here my intention is to return a url of a file which is available in server.Client can access the file using this url. – Anoop M Nair Sep 03 '15 at 07:12
  • In general the files are mapped to URLs by your server software. You need to generate **that** URL. – Dakshinamurthy Karra Sep 03 '15 at 07:17
  • @KDM I didn't get you ,Could you please explain it,here my application server is tomcat and I am using Spring REST for server API – Anoop M Nair Sep 03 '15 at 07:20

1 Answers1

2

Okay, Assuming that your server IP is 192.168.3.37 and your upload API will put the uploaded file into D:\Anoop\pic\ on the server. So what you have todo is to expose D:\Anoop\pic\ via protocols like HTTP or FTP.

Than you will have something like http://192.168.3.37/pics mapped to D:\Anoop\pic. This way, all file inside the directory were exposed using HTTP. You can do the same strategy with FTP.

So, what you return to the client is giving the URL to any speciffic file that was uploaded before, if you have uploaded the file 2unvvhlacq5fh09tokr7i25cvj.jpg and the server API put it on D:\Anoop\pic\2unvvhlacq5fh09tokr7i25cvj.jpg then it will be accessible with http://192.168.3.37/pics/2unvvhlacq5fh09tokr7i25cvj.jpg

I hope you get the idea.

Ferdinand Neman
  • 680
  • 3
  • 6
  • One more doubt ,how to expose D:\Anoop\pic\ via HTTP? – Anoop M Nair Sep 03 '15 at 08:06
  • Well, it is a subject of another question... But as a hint for you, you can create another servlet that will expose the file on external location as explained [Here](http://balusc.blogspot.my/2007/07/fileservlet.html) **OR** Use other HTTP server such as Apache HTTPD to expose the folder **OR** Have the file uploaded into some directory inside your webapp folder. And this [link](http://stackoverflow.com/questions/417658/how-to-config-tomcat-to-serve-images-from-an-external-folder-outside-webapps) address the same question as yours. – Ferdinand Neman Sep 03 '15 at 08:20