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.
Related
My application uses wicket 6.3 and my file upload is working as expected, except for some cases of corrupted or distorted files when viewed or downloaded.
In most cases, file upload is working, but there are cases when the uploaded files are saved (no error), but cannot be viewed or downloaded correctly since it is corrupted.
The file size of these files are just less than 100KB.
When I say corrupted, when you try to open the uploaded file it will look like this:
Below is the part of my code that saves the uploaded file:
BE code
FileUploadField fileUploadField = new FileUploadField("fileUploadField");
...
// File types can be images (jpg/png/bmp), documents (docx/pdf)
// File types with random distorted/corrupted (png and docx)
// Reuploading the file will fix the issue
for (FileUpload fu : m_fileUpload.getFileUploads()) {
byte[] fileByes = fu.getBytes();
String fileId = myService.persistFile(new MyFile().setContent(fileByes));
supportingDoc.addAttachment(fileId, fu.getClientFileName(), fileByes.length);
}
...
HTML
<input wicket:id="fileUploadField" type="file" class="form-control">
When I try to replicate it again, it usually works, and I can download or view the file.
I am unsure why it gets corrupted/distorted and how I can avoid getting a such an error though it rarely happens.
Update:
I thought the initial case was a PNG upload but it was not, it was docx. When I tried to replicate the issue using PNG file. It works.
I have 2 tomcat servers (test and live). I uploaded the same docx file on both tomcat servers (both on ubuntu). Test server was able to view/download the uploaded docx file, while Live server did not.
I am converting the file into byte array and save it to DB. When I compare both file contents in DB, they have exactly the same content. So the problem is not really on uploads.
I think the problem is on the download decoding, both servers does not have the same decoder. On my local environment (tomcat + windows) it works, same with my Test environment (tomcat + ubuntu). My Live environment running tomcat + ubuntu seems to have a different default decoder that is why it cannot view/download docx properly.
My problem now is where and how I can check that default decoder? Will I check it in ubuntu side? or it should be in tomcat side? When I checked the tomcat server config on both Test and Live tomcats, both seems to have the same config. They only differ on SSL certs.
Solution that works on my case:
The problem was really on how my Live server handles unknown mime-types. It is handling it like text file. This is the reason why it is showing the unknown mime-type file as garbled text.
I checked my Live tomcat server configuration and compared it with my test server's config and both are almost the same and I cannot find any configuration relating to mime-type or encoder/decoder.
In my file download code, when mime-type is unknown it is setting it as force-download only so I changed it to application/force-download.
For unknown mime-type, I changed the content-disposition from inline; filename="<filename>" to attachment; filename="<filename>".
I think content disposition attachment alone works, I haven't tested it though since application/force-download seems to be a hack, while setting the correct content disposition as attachment will download the unknown mime-type for my case.
I will not delete this question though, since I might have the same issue in the future and I forgot how I solved it :)
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
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.
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...)
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.