Loading properties - java

So i have a file called app.properties which contains to urls in the format of
somethingurl=http://.../.../.../something.js
Note:there is actual url
But i switched to an internet less environment and cannot get the files.
So instead, i replaced the urls with actual paths to the files that i downloaded.
But i get a java.net.MalformedURLException: no protocol. So now i have the files that urls were pointing and i try to use them create an instance of JSFactory.
So is there a way to make the paths work?
I use to jsoup.connect(app.properties.getProperty("...")).ignoreContentTyp(true).execute().body();
Can i still use file: protocol?

Use file protocol. file:///path/to/your/file.js

Related

Java - get real filename of symlink on remote HTTP Server

I am working within Java, and downloading files from a HTTP Server. Now we are working with symlinks here, so we do not need to change the http link - it is always pointing to "last-uploaded.zip" which is linked to the last uploaded zip file, as an example "package43.zip".
Do I have the chance to get the original filename within java? So the link is pointing to "last-uploaded.zip" but if it is downloaded I want to rename it to "package$version.zip".
Regards,
Marco
No. The whole symlink concept doesn't transfer over HTTP, so when you make a HTTP GET for last-uploaded.zip you don't know if it's a file, a symlink or just an endpoint that returns bytes.
The simplest solution is probably opening the zip and searching for the version number from inside there somewhere.

Get Absolute path of a url in java

Is there a way to retrieve the absolute path of url (http://localhost:8080/myApp) in java. The scenario is, i need to connect to csv file located in tomcat server. The statement works well if I enter the absolute path, but is there a solution to retrieve url's path using getAbsolutePath().Sorry if I'm wrong.
Connection conn = DriverManager(getConnection("jdbc:relique:csv:/home/apache-tomcat-6.0.26/webapps/myApp/"))
Thanks in advance.
You can use ServletContext.getRealPath(), which does exactly what you want.
Note that it does not necessarily work in all situations. For example, if your Tomcat is configured to deploy the .war file without unpacking it, then this will return null.
I don't know much about JAVA.
May be getServletContext().getContextPath() is something you are looking for
EDIT:
Or may be getRealPath()
Tomcat is not a http server. All tomcat urls reference services, not files.
You'll have to implement another service that sends the csv file on request, if you want to get it through any http URL. URL's like http://localhost/myapp/input.csv require a http server like apache httpd.
(Hope I got your question correct...)

how to find out if the url http://www.test.com/abc.pdf is a file or a directory?

i want to write some code in java to find out if a given url is a file or a directory. how can i do this??
URLs themselves don't have the concept of being a "file" or a "directory". The content of a URL is defined by whatever the server responds with when requested. If you get something with a MIME type of application/pdf, then the URL represents a PDF file. If you get anything else, then it's not a PDF file.
There is simply no notion of a "directory" in any of the URL / URI specs, the HTTP specs or the MIME type registry.
So the webserver has no way of telling the client that a URL resolves to a directory ... even if it knows what that means. (And in many cases, the webserver doesn't know / care about directories itself; e.g. a typical RESTful web API doesn't recognize the concept.)
Your options are:
Try to fetch things and see what content type you get. But bear in mind that a "directory" might be rendered by the webserver as anything ... so it is (in general) impossible to reliably distinguish directories from non-directories this way.
If you wanted to avoid downloading the file, you could send a HEAD request instead of a GET request. This requires the use of a fully fledged HTTP client library rather than URLConnection.
Change your application design and implementation so that the "directory" concept is not required.
Change your application so that it decides what is a "directory" and what is a "file" based purely on the URLs. (In the general case, this won't work ... because there are no universally observed conventions for URL name parts that would allow you to make the distinction.)
Change to using a URL scheme / protocol in which "directory" is a well-defined concept. For instance "file:" or "ftp:".
What you get back from a URL essentially isn't a "file" or a "directory." At best, it's a stream of data with a content type. It generally becomes a "file" on the client side, either by means of saving it to a file system or to a temporary store for display only. Basically, there's no way for a web server to tell a client that something is a directory using HTTP.
You're either going to have to create some client-side business logic to infer a "directory" (possibly based on the URL, maybe a lack of file extension?) or use a different protocol for this.

getFilePath API of FilePart not working as expected

I am working on a webbased application with servlets and JSPs in it. My requirement is to get the path of a file which is uploaded in my application.
The legacy code used to get the name of the file by using the code -
//FilePart class of the com.oreilly.servlet.multipart package.//
FilePart filePart = (FilePart) part;
screenosInputFileName = filePart.getFileName();
The getFileName returns the name of the file correctly as a string like "a.txt". Since I want the path also, I am using getFilePath as in--
String path = filePart.getFilePath();
However, I find that getFilePath is only returning the file name and not the file path. That is, getFileName and getFilePath are returning the same value "a.txt". What I was expecting from getFilePath was something like c:\myfiles.
Also, I am running my application in an Ubuntu enviroment (a linux flavour).
Any ideas why getFilePath is retuning only the filename and not the file path ? And how to overcome the problem. Any pointers higly appreciated.
Note: I am not familiar with com.oreilly.servlet.multipart.FilePart.
If FilePart represents the file on the client, then it is impossible to get the path of it (there is no reason for a server to know whether a.txt was uploaded from C:\Users\bob\ or from /home/bob/Documents/, so that information is not included).
If FilePart represents the file on the server (if your server saves uploaded files to a temporary directory so that you may access them as actual files), then you should be able to use this to get the actual path to the file:
String path = new File(filePart.getFilePath()).getAbsolutePath();
I hope this was helpful!
It used to be that Internet Explorer would include the full path of the file on the client's computer. I don't know if it does that anymore, but i don't think so, because it's a privacy issue. The server has no business knowing the full path.
getFilePath is only going to work if the client is using Internet Explorer, as it's the only browser that returns the entire file path to the server. Chances are, it'll only work with IE6 at that, as I believe MS finally realized this wasn't a good security practice when IE7 came out.

how to access directory from file server in java?

HI...
Currently I m working in a application in which application allows to access directory (which contains some files) from file server to Application (client).
I tried following code..
URL url=("http://192.168.5.555/file-server/user/images/");
URI uri=url.toURI();
File list[];
list= new File(uri).listFiles();
But its thrown java.lang.IllegalArgumentException Exception.
I don't know how this happen?
I simply access images directory from the given URL (file server).
Help me...
That isn't going to work. The java.io.File operates on the local disk file system only, i.e. on URI's starting with file:// only. It would otherwise indeed going to be too easy to leech files from places where you aren't allowed to do so.
Check if the server in question supports FTP, then you can just use FTPClient#listFiles() for this. If it doesn't, but it supports directory listing, then you need to parse the HTML response containing the directory listing with a HTML parser like Jsoup and then refire a new request on every found link.
If it doesn't support FTP or directory listing, then you're lost and you're probably trying to do bad things.

Categories