Need to get a path location via a web page - java

In firefox 2 I was able to get the path using Browse - I use the path in our project to then write out files to that location. So now that browse does not get the path, does anyone know a way for a user to go to a directory and have the path returned via a web page so I could pass that along to the server for processing?
execCommand does not work in firefox and had limites save type capaility, and entering by hand is not a useable option. Thanks.

The ability to see a complete client file path is now considered a security risk, and all modern browsers prevent you from seeing it (both via Javascript and via information sent back to the server on the form POST).

This is not possible with HTML/JavaScript. In HTML you can at highest use <input type="file"> to select a file, but not a folder or so. In JS you can't do anything at the local disk file system, let alone with a <input type="file"> element in the DOM tree. You're prohibited by security restrictions (you as being an enduser would of course not like if websites are able to do stuff at the local disk file system unaskingly).
You can only do that with a small application which runs straight at the client machine. For example a (signed!) applet which is basically just a piece of Java code served by a webpage which runs right at the client machine. You can communicate between applet and servlet using java.net.URL and consorts. Then, in the applet use Swing's JFileChooser to have a folder or file selection dialogue.
Update: by the way, MSIE and some other ancient browsers sends the full client-side disk file system path along the <input type="file"> to the server side. This is technically wrong (only the filename+extension should have been sent) and completely superfluous. This information is worthless in the server side, because it cannot access the file using the normal java.io.File stuff (unless both the server and the client runs at physically the same machine which of course wouldn't occur in real world). The normal way to get the uploaded file is to parse the multipart/form-data request body (for which one would normally use Apache Commons FileUpload or the Servlet 3.0 provided HttpServletRequest#getParts()).

Related

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.

Vaadin: How to check if browser has pdf plug-in?

Is there a posability in Vaadin 7 or in Java generally, to check if a browser has an embedded pdfreader or not?
I need to know that because it depends on that how i open the pdf.
There is unfortunately no way to consistently check if the browser supports viewing PDF files or not. I would recommend using something like PDF.JS (https://github.com/mozilla/pdf.js) or FlexPaper (http://flexpaper.devaldi.com/products.jsp) on your web site to display your documents to make sure your visitors can see your documents
Both those options are available as open source
In a web application, Java (and per se, Vaadin) runs on server side, so you cannot know which technology is installed on client, in this case, the browser. Just fire your file download with the application/pdf mime header and let the client do it's work. If you want to fire it as a general file download, use application/octet-stream mime header instead.
Here's a more generic q/a on this topic: How to determine if the user's browser can view PDF files

Read local file without open dialog (in browser)

There is any way (from browser) to read local file without open dialog?
Can be used: javascript, flash, or java.
Goal: when you paste text in WYSIWYG with images from MS Word, there will be links to image files. Like "file:///C:/Users/username/AppData/Local/Temp/msohtmlclip1/01/clip_image001.jpg". I want to read this files, upload to server and replace local path.
Accessing the disk contents from the browser without the user permission is a security risk. As such, to do that with Java you need your applet signed and the user allowing it to run.
A Java applet can do this, but it would need to be signed with an SSL certificate from a trusted authority, otherwise the applet would not be run with a security exception thrown.
You'd use any standard Java approach to loading and uploading the file (Apache Commons FileUtils and HttpClient?); if you're new to Java then there may be simpler solutions.

java tomcat links generated don't work

I'm just doing a java tomcat project, that does some query in a database then return the file path of some web pages.
Now I have mapped my only class in web.xml and the webapp does return a list of urls which correspond to some html pages in my local disk. I set up a side frame in the webapp, my idea is that I output the results in the output page like "file:///file_path_of_html_page" and when this link is clicked, the side frame will show the html page.
But actually I got the right links but when I click on them, nothing happens, chrome tells me "Not allowed to load local resource". Even I set the target="_blank", the link doesn't work. But the "file:///filepath" are all ok when I type them in the address bar. I've moved all the html pages in the eclipse project folder but that didn't help.
Any suggestions to do this simple task?
The average browser disallows due to security reasons opening file:// resources when the parent resource is by itself served over http://. If you make them fullworthy http:// links, then it will work properly.
Even if the browser allowed it, this approach would not going to work when you publish the webapp on a different server. A file:// resource refers to the local disk file system, which is the one the client (the user with the webbrowser) is using. This is normally in a physically different machine. The client should have a copy of exactly those resources on its own local disk file system beforehand in order to get the file:// links to work.
Just put the HTML pages in public web root of your web project (there where you normally put your JSP files and so on) and use (relative) http:// links to refer the HTML pages. For example, the following link in a http://localhost:8080/contextname/some.jsp
link to some html file
would open the http://localhost:8080/contextname/some.html file.

Java Web Application on Tomcat - Displaying a File Uploaded by User

I need to display uploaded files on a web application, the flow is as follows
User uploads file through web UI
Validation on image
Makes call to imageRepository.store( uploadedImage, user.getSite() )
The user wants the image displayed which adds <img src="${anUploadedImage.getUrl()}"/>
but that is where I'm stuck, what can getUrl() do? The simple solution is to put it someplace and let Apache serve the file, but then I can't use the application to ensure that one user isn't modifying the URL to view other users files, which in this case is important
You have a couple of options if you want to manage the path to the file.
You can create a service to return a file by ID. Essentially this controller-action (or servlet) would load the image back from the repository store and serialize it back to the user. This provides the most control.
You can create a filter that performs the control you want, then delegates back to the web server to perform the actual file serving. This still gives you control, but takes the responsibility off of your app to serialize the file efficiently.
The one thing I would want to verify (since I'm not sure what type the ${anUploadedImage} is) is what URL getUrl() is returning. If it is a File object, then getUrl() provides a file URL which is useless in the HTML. Essentially the web browser would go looking on the user's drive or report some sort of fishy activity.
The URL you include in your HTML needs to point to where your service will send the desired image.

Categories