How to read & open multiple file in one request using struts? - java

I have a requirement that, User will see the list of files in the page which is the list from directory. From there they will select multiple files and they click on view button. Then we need to read the corresponding files from Drive and need to open all the files which are all user has selected. I need to implement this using Struts2/servlet.

From what I userstand the user sees the content of a directory which is located on the server, right?
Further, you display a list of files along with some selection method (checkboxes ?) which the user uses to select files and then clicks the view button.
If I am correct, you could use javascript to open a new tab/window or dialog (e.g. a jQuery dialog) which reads the file or its contents from the server. Just iterate over the selected entries in the list and pass them one by one to the JavaScript function that opens the tab/window/dialog.

It is not clear if you do want to
open each file in the client's browser, or
download each file to the client's file system, or
download an archive containing all the files to the client's file system;
In 1. or 2. you need to create multiple requests, targeting multiple windows, using target="_blank", or by AJAX, etc .
By manipulating HttpResponse Header objects, you can instruct the browser when to open or download a file: Content-Disposition: inline; will try to open it inside the browser, while Content-Disposition: attachment; will perform the default operation (open with default desktop application, download, or ask if none of the previous was marked as default by the user)
Keep in mind that you may encounter several possible problems by opening or downloading multiple files at once; it could overload/crash the client, overload/crash the server, cause problem of bandwidth etc; what if the user choose 100 files ? You would open 100 tab, or 100 windows, or ask 100 times where to save each file, etc...
If you want to create a dynamic ZIP instead, you can find a kick-off example on how to do it in Struts2 here, (using an Action like a Servlet, and returning NONE);
My files were from database, yours are from file system, but the code is the same.

Related

JSP: save a file on client without storing it on server

In my jsp someone wants to export a query result to csv. Can't use frameworks. At the moment, here is the process:
press button, go to servlet
prepare data for csv
make csv and save to server
back to jsp and let them download the fresh-made file from an anchor tag.
Thing is, I' don't want to create this file on server, as I have to dispose it afterwards, but I still want to give the user the "save as" window. So, is there a way, putting for example an OutputStream object in session, to achieve this result?
Thank you all!
A servlet can generate any type of content. So, when you click the button to run the servlet, simply have the servlet write the file back to the client at that time. You'll need to set the Content-type header to "text/csv" (and making sure that you set encoding properly). You don't need to set the Content-Length header; the browser can deal with that.
When the servlet returns data to the browser, the user will be prompted to save the file or open it with an application.
Yes. You don't need to save the file. Just hold it in the session and send it in-memory to the OutputStream. I would trash it after a given time or after delivery in order to save memory.
Set the content type and the content disposition appropriately. This will mean that the browser interprets the output correctly and prompts your user to save it or launch the appropriate application.
See this SO answer for more details.

JSP Limit the directories of a File Open dialog

I am currently coding a program using the language JSP. I was wondering if/how you would restrict the directories a File Open dialogue box can browse, for instance:
The default directory is in C:/userFiles/username/ I want the user to be able to browse files in that /username/ directory, or directories within that directory, but not any directories above like C:/, or C:/userFiles. I have done some research, and I could not come across an answer. Is this possible with JSP, HTML, or Java and if so, how would I do it? I would prefer JSP or HTML.
Additional Info:
The dialogue box would browse files on a remote server, not on the user's computer.
The File Open dialog is on the client machine and is part of the operating system. You cannot change its behaviour.
In order to list files on a SERVER you would not use a File Open dialog, you'd be turning the list of files into HTML and serving that to the browser. Since you're the one writing the server-side code, you are ALREADY the one in control over which files make it into that list in the first place.
Saying you'd prefer "JSP or HTML" is a pretty good indicator that you don't fully understand the client-server relationship of the web.

Access a file from different Directory

I am working with a Java Application. In this, I must copy a file from source to Multiple Destinations. The destinations are various USB drives. (ie) I'm copying a file for desktop and sending to a number of USB drives. Here, my problem is, I am sending files to all USB drives I attached, now I need to make the files UN-deletable. The undeletable process will be done in a single event. For example once I click the ok button, the Files, which are present in all USB drives are made undeletable. Any idea how to do this.
When copying source file to other files, put the destination File objects in a collection. Then, in the event handler for your 'set read only' button, loop over that collection and call setReadOnly() on each File.
not very sure I understand your question correctly.
maybe you can store the directories and when you click the OK button, you iterate these directories and set them to undeletable one by one?

Informing the user that the file is being generated in a download servlet

I have a download servlet which generates a ZIP with some files, one of them pretty big, and then sends the generated file in the response for download.
The problem is that the generating process is pretty big, and between the step of the generation of de ZIP and the download step (when the user see the download dialog) many seconds or even a minute could pass. So I would like to inform the user by anyway that the file is being generated.
The solution I'm thinking about is doing several requests, one for open a modal window which informs the user and inside this, then do another request automatically to an action which generates the file in a temp location and then, when the request ends, another one which closes the window (the window must be closed automatically) and then request to the download servlet the generated file in the last step.
If somebody understands what I'm trying to do, I would like if exists a better and cleaner solution.
You can fire an ajax request which will start the generation process, and then other ajax requests to poll the server if the file is ready. If yes - change the location of the browser to the file. Otherwise, show a "loading" message/image/..

In java web application how to print server file from client side

In the java web application need to select the file from server and print to the local printer. how it can be done
Thanks in advance
That's going to be tricky whenever you require a minimum of user interaction (i.e. just click the link and then do the print magic) and it also depends on the type of the file in question. If it is for example a .doc file, then you would basically need to download it to the client environment and open it in the default associated application (MS Word in this case) and then let the application execute the print command. You can't do this from the server side on.
Your best bet is to create an Applet which in turn displays the file tree, downloads the file to the local disk file system on client interaction and makes use of Desktop#print() to print it. E.g.
File file = new File("/temp/file.doc");
// Read file from server using URLConnection, write it to this file and then do:
Desktop.print(file);
But if it are for example plain text files such as text/html, text/xml, etcetera, then you can make use Javascript to load the file into some <div> or <iframe> element and then execute the window.print() method on it, if necessary along with a CSS media rule.
You will need an applet, flash, silverlight, javafx - i.e. an embedded app. There:
download the file from the server by creating a GET request (in an applet - using URL.openConnection()), obtaining the returned bytes and forming an in-memory document
sending that to a printer. If you chose applet - this might help
(I'm not aware whether the same flow can't be achieved with javascript as well)

Categories

Resources