How to get full file path of uploaded file in Spring - java

Hi I am trying to get the full file path of a file that I uploaded in Spring. I am able to get file name by using bean.getFile().getOriginalFilename(). But this gives just the file name like abc.csv.
I tried searching answer for this in several forums and I am not finding proper way of retrieving file path information.

Modern browsers do not send full path info for file uploads. They only send the short file name. There's no way to retrieve it if the client doesn't send it.

Related

Java Zip file Export Options

My requirement is:
User will upload a .zip file (which will contain multiple files) via API and I need to send back the files to the user in response.
My solution 1:
returning list of the file download link to user in response.
but the user is not happy with this solution.
Is there any other way return the list of file?
Alternative solution would be to unzip your archive directly in browser using a library like zip.js or similar.

How to upload and download a file with the given path(already known) without using html form in servlet using post method?

I know solution of the above problem using HTML form in which user chooses a file from computer and upload it and then download it but how to do it without using HTML with the already given path of file.
Thank you For your concern in Advance.
I assume the scenario is that the user accesses your server from a browser.
In this case you will not be able to access the file system given file paths. Only the user can grant access to certain files by selecting them in a file upload input.
If the user uses a mobile/desktop application that sends the file to your server, you must implement the upload in the app itself.

download file from html page not working [duplicate]

This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 7 years ago.
I uploaded new app on Jboss 7.
That app, amoung other things, can create file, save it and hopfully download it with html5 tag.
After generated, the file saves on absolute path that I get from
getServletContext().getRealPath("/");
By the server log I can tell that those actions done perfectly.
The file is created and saved.
The problem is with the download part.
I am trying to download the file with html5 tag.
<a href=path+file name> download>Get Numbers!</a>
I am using exactly the same path that I used to save the file on the server and I keep getting that fail-no file error from Chrome.
Ideas?
I am using exactly the same path that I used to save the file on the server
There's the problem. Your file path is something like this:
/opt/repo/versions/7.1/standalone/tmp/vfs/tempc56e386fb58c08a8/SlL.war-269‌​016b5c31c942c/serial.xls
That's fine on the server when getting the file from the file system. But that path means nothing to a web browser. The web browser is making a request to the web server for a file known to the web server.
So, for example, if your web server root is here:
/opt/mywebserver
Then that path ends up requesting this:
/opt/webserver/opt/repo/versions/7.1/standalone/tmp/vfs/tempc56e386fb58c08a8/SlL.war-269‌​016b5c31c942c/serial.xls
That file doesn't exist, so the web server responds with a 404 error.
You need to convert the file system path to a URL in order to use it in the markup. (And that path needs to be publicly visible to the web server, within its own path structure.)

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