How to get local file in HTML when using JSP and Java - java

I'm running a Java web application using Tomcat and I've got my Java classes and .jsp files rendering the web. I am trying to implement a feature where you search for key words within files and then return the files so the user can download them from the webpage.
I want to store the files within a local directory, as the server will eventually be running off a local Linux machine. However, I am unable to get the path to the file to work on the HTML side. In Java I can easily do :
final File file = new File("/home/user/resources");
Which will find the files in there. However, in HTML I've been trying various URL's, including:
a href="/home/user/resource" download>"Name"</a>
However it cannot find the file, I gather this is because it's looking within the web application.
So, my question is. How can I get the HTML side to find the same files, in the same location as Java? If it's not possible without storing the files within the web app, is there a way to serve up the file from the Java side and make it available to HTML?
Also, the files I'll be storing will most likely be Word files, .doc and .docx.
Thanks for your help in advance.
Let me know if you need any more info.
EDIT : Updated download link within the .jsp file and within <% %> tags.
<a href="file:///home/simon/resources/clientInfo/" + entry.getValue() + "\" download>" + entry.getValue() + "</a>
Chrome complains of a network error.

You can do it with file scheme. Without the file scheme the browser assumes that your file is relative to the webpage and tries to resolve the file by making a request to your webapp.The file or link needs file:/// added before the your jsp's <%=path%> variable, so that the browser knows it needs to open a local file on the user's machine.
Sample link
click here

Related

Run applet java in html

I have an applet java displaying images from folder ,i cant run this applet using html i have this error :
The error you are getting is because your applet is trying to read a file from a directory in the local system where it is getting downloaded. This is a standard security feature. This of this way, you go to a website which downloads an applet and read a file or executes an .exe in your local system. Naturally this should not be allowed for an applet / javascript or code served from a server.
You need to create a signed applet. Please go through the following link to accomplish your need :
http://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm

Read a file from remote system using jsp

We can read a text file using JAVA program on a localhost.
My problem is , I want to read a file using JSP which is on a client's system . When he runs a JSP using his browser.This JSP should read the file.
Is this possible or not?
And if yes , how this can be achieved?
No, it's not possible.
First of all, because the JSP is excuted on the server, and not on the client machine.
Second, because if a random web page could read files on your computer, it would be a major security risk. You can let the user choose a file and upload it to your web server, though.
Also, JSPs are view components. Their goal is to generate markup, and they shouldn't contain any line of Java code: only the JSP EL, and JSP tags. For anything else, use Java files.
Sorry I cant just comment. But, wouldn't there be a restriction on the browser not being able to access files on the client?
Perhaps the way to do this is to prompt for the file to be uploaded to be processed.

Preselect files in JUpload

I have a requirement to process an external request to populate a HTML form with the parameters mentioned in the URL. This part is working fine. However, the URL also contains paths to files present on the client machine and I want to upload those files from the client machine to the server without user interaction.
Since it is not possible with HTML/Javascript to programatically select files, I tried using the Applet approach using JUpload. However, I am not able to figure out, how to preselect a file on applet initialization. It is not necessary to upload the files right away, but I want atleast to select the files automatically. User can review the info and then submit the form. and files in the applet.
Is it possible with this library? Or direct me to some better path
OK, so I found my answer in a different library with similar name. With Smartwerkz JUpload we can pass a parameter preselectedFiles="filePath" and autostartUpload=true to preselect files and auto upload files without user interaction. I hope it will help someone someday.

How to display directory in Spring App?

So I'm writing a Spring web application that runs some scripts on the local server using ProcessBuilder. That part seems to be running fine. The scripts generate output files and then zip them up into a single .zip file. I'd like to provide the user with a way to download these files but I'm not sure of the best way to do this, or even how to implement any way to do it.
I tried putting the path to the directory into the URL, but I believe since Spring intercepts all URLs and it doesn't know how to process the one pointing at either the directory the Zip is in or the Zip itself, I just get an error. What would the proper way to do this be? Either to display a list of files or just link to the files themselves? Any help would be really appreciated, thank you.
The way you do any file download from a web-app is to write a servlet that writes the content of the file to the http response's output stream. There's an example of this here that's downloading an Excel file.
The directory listings that you get in a web-page don't just happen automatically, you'll have to write a JSP that displays the directory listing with hyperlinks on each file that link to file download servlet.
I looked around and could not find a complete directory listing example for spring. Though it is relatively simple to implement I put together a blog post to explain this, so that it could be quickly reused when needed http://krishna-passionatelycurious.blogspot.com/2013/04/file-download-page-using-spring.html.
But, the overall approach is to loop through the contents of a directory and generate appropriate links, such that the links point back to the same spring handler, where we could make a decision based on whether the accessed link actually points to a directory or a file and generate directory listing again or stream the content for download.
The blog has a sample implementation of this.

How to preview a file on the server in JBoss

I need some ideas on how I can best solve this problem.
I have a JBoss Seam application running on JBoss 4.3.3
What a small portion of this application does is generate an html and a pdf document based on an Open Office template.
The files that are generated I put inside /tmp/ on the filesystem.
I have tried with System.getProperties("tmp.dir") and some other options, and they always return $JBOSS_HOME/bin
I would like to choose the path $JBOSS_HOME/$DEPLOY/myEAR.ear/myWAR.war/WhateverLocationHere/
However, I don't know how I can programatically choose path without giving an absolute path, or setting $JBOSS_HOME and $DEPLOY.
Anybody know how I can do this?
The second question;
I want to easily preview these generated files. Either through JavaScript, or whatever is the easiest way. However, JavaScript cannot access the filesystem on the server, so I cannot open the file through JavaScript.
Any easy solutions out there?
Not sure how you are generating your PDFs, but if possible, skip the disk IO all together, stash the PDF content in a byte[] and flush it out to the user in a servlet setting the mime type to application/pdf* that responds to a URL which is specified by a link in your client or dynamically set in a <div> by javascript. You're probably taking the memory hit anyways, and in addition to skipping the IO, you don't have to worry about deleting the tmp files when you're done with the preview.
*****I think this is right. Need to look it up.
Not sure I have a complete grasp of what you are trying to achieve, but I'll give it a try anyway:
My assumption is that your final goal is to make some files (PDF, HTML) available to end users via a web application.
In that case, why not have Apache serve those file to the end users, so you only need your JBOSS application to know the path of a directory that is mapped to an Apache virtual host.
So basically, create a file and save it as /var/www/html/myappfiles/tempfile.pdf (the folder your application knows), and then provide http://mydomain.com/myappfiles (an Apache virtual host) to your users. The rest will be done by the web server.
You will have to set an environment variable or system property to let your application know where your folder resides (/var/www/html/myappfiles/ in this example).
Hopefully I was not way off :)
I agree with Peter (yo Pete!). Put the directory outside of your WAR and setup an environment variable pointing to this. Have a read of this post by Jacob Orshalick about how to configure environment variables in Seam :
As for previewing PDFs, have a look at how Google Docs handles previewing PDFs - it displays them as an image. To do this with Java check out the Sun PDF Renderer.
I'm not sure if this works in JBoss, given that you want a path inside a WAR archive, but you could try using ServletContext.getRealPath(String).
However, I personally would not want generated files to be inside my deployed application; instead I would configure an external data directory somewhere like $JBOSS_HOME/server/default/data/myapp
First, most platforms use java.io.tmpdir to set a temporary directory. Some servlet containers redefine this property to be something underneath their tree. Why do you care where the file gets written?
Second, I agree with Nicholas: After generating the PDF on the server side, you can generate a URL that, when clicked, sends the file to the browser. If you use MIME type application/pdf, the browser should do the right thing with it.

Categories