Java - get real filename of symlink on remote HTTP Server - java

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.

Related

How do I append some text to a distant txt file via http in Java?

A text file is located at http://example.com/myText.txt with read and write permissions.
How can I append some line of text on top of it using Java. I see examples with the class OutputStreamWriter but they all use a file as an output. What about http?
(I need the code to be compatible with Java 5)
HTTP in the sense you understand it does not support any write operation of this nature.
There is no clear relationship between a URL and a file on the server; Specifically HTTP GET generally returns you a file-like resource if the URL refers to a web-like server; but the outcomes of HTTP POST are not simply an overwrite of the remote url file they are processed on a case by case basis by the other side.
If this is really what you want I strongly suggest you use FTP which is intended for this purpose.
There is no generic solution to this other than reading the content, appending the line locally, and writing it back with PUT (and that requires that the server supports the PUT method).

Can I compare two files: local and remote on http server without downloading?

I want to compare two files (one file is located on local location and second is located on http server). Can I do that without downloading? I want to know that two files is completely the same.
Hash the files in both sides
Get the hash from the server
Compare to local file's hash
Depends on what level of comparison you need. I assume you don't want to upload your local copy to the server either. You could easily take a hash of the file using MD5 or SHA1 and send that hash to the server to compare the files. If the hashes match they are the same. If they differ then you could then choose to upload the file for diff type comparison.
You can't.
You can create a MD5 or SHA1 checksum und put it next to your file onto the server. Than you'll have to download that file.
You may use http-caching mechanisms (e-tag etc.).
I can't understand why you need this kind of thing ..its not usual condition .
Firstly the file which is locating in your local server (I guess you can get the size of it )
and the file which is on the http server you need to make web service which can provide you the size of the file ..
so you can easily compare the size of tow file .
I use an app called Beyond Compare to do exactly that. http://www.scootersoftware.com/

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...)

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