Upload files in JSP/Servlet - java

i know how upload files in jsp/servlet, i already use fileupload from apache, but i don't know how to return some status of the upload to the user, for example, actually when someone select the file to upload, they have to click in 'upload' button and just when the upload finish the jsp page redirect to the another page.
I want they can see a percentage of how much will take to complete the upload operation.
i already implement a listener using fileupload, but i don't know how to show this information to the user, 'cause just when the upload is finished that i can redirect to another jsp page.
Any ideas ?
Best regards,
Valter Henrique.

That's not possible in combination with plain HTML <form> with <input type="file">. To the point, you've got to send multiple simultaneous requests. A form submit with file upload will namely block as long as the upload is in progress.
Your best bet is using JavaScript/Ajax and/or Flash. There are several free file upload components supporting a progress bar, like Uploadify and SwfUpload.
If your servlet is well written, you should be able to use it unchanged.

Related

Dialog box in Spring MVC controller

Is there a way to display a (preferably modal) dialog box in an servlet controller ? If it isn't modal that is something I can deal with (!?) as long as it initially appears above the browser.
Essentially I have a form with a table and 2 buttons on it. One button takes the user to a different place in the workflow and is irrelevant to this question (just to explain why the other one doesn't 'go anywhere').
The other button currently goes back to an MVC controller, calls some code to export the table to excel and then reloads the web page. This is all working okay except the way I am calling the dialog box is calling it underneath the browser. I suspect this is because I am sending null as the frame but I'm not sure what to put in its place ?
JOptionPane.showMessageDialog(null, "Export Completed.", "Excel Export", JOptionPane.INFORMATION_MESSAGE);
Many thanks.
I am not too sure what you are trying to achieve from your question and without any code examples. However, presumably what you are doing (guessing here), is that you are trying to export some data from some data source and convert it to an Excel file. You have to keep 3 things in mind.
Web applications work via HTTP requests and responses. So the only thing a Servlet can do is send back an HTTP response that indicates that the export was successful. Whether you use a traditional page, or maybe use AJAX to avoid refreshing the page is purely your design choice. You could also start with a simple page and then change to AJAX combined with JQuery later once you get used to what is happening.
Exporting the excel sheet to the server does not mean that your client has access to it. Speculating here, but you will probably need a mechanism for your user to get the file. One simple approach used by many webapps is to actually send the Excel file as the Servlet response itself. So what would happen is that when the export is completed the browser starts receiving the file and the user sees it downloading. From your servlet you will just need to set the right mime-type and set the content-disposition header to state that the file is an attachment (so that the browser downloads it as a file).
httpresp.setContentType("text/csv");
httpresp.setHeader("Content-disposition", "attachment; filename=\"export.csv\"");
You will probably also need to set the file size. There are various full examples on SO if you look for further details.
When performing these operations remember that users can interrupt the browser or refresh. If the user presses F5 he might cause your application to do the export again. One common approach to this is called 'redirect-after-post'. Basically you redirect the user to a page which just displays the outcome, without performing the operation again. This way if he presses refresh, he is just refreshing the page with the message.

Uploading multiple files on the server Using JSP

I have often wonder how Facebook is able to handle uploading multiple files on the server when I am uploading my pictures.
I am quite not sure how it is being implemented. As I know, you could only send one file to the server through http one at a time unless you are going to make use of Applets.
Does anybody know how Facebook implements this? Is this Flash or an applet or something?
There's nothing special that you need to do on your web page - multiple <input type="file"> elements in the same <form> will upload multiple files at once.
The tricky part is handling all those files on the server. Take a look at a library such as Apache Commons FileUpload
Edit
You might want to take a look at this thread - people have suggested quite a few readily available components that you can use (note that these are for the client-side i.e. in the browser. You still need to handle the uploaded files on the server using something like the FileUpload library I mentioned before)

Java file upload

can i upload a file(image) without submitting the form..
Basically i want to create a webpage where a user can upload an image and preview it side by side but this should not submit my form.
i dont wanna use jquery
You can have two forms on the page. One to upload the image and other one for something else. Just don't nest them.
I've tried to implement this with AJAX but, if it's even possible, it would be a nasty hack. About the best you can do reasonably is to put the file upload form in an iframe, and after posting the file with AJAX, redraw the page with the image.
I think this is what you want But it uses PHP for sever side you can use what ever you want!
All you have to do is create a page which will handle the ajax call, which is saving the image and returning string "true" in case of succeeds. and for Java this (Handling Form-based File Upload with Java Servlet or JSP) should help you.
Check this one out ioncannon.net
You can try this .Also check

Struts: Upload a file with progress bar

i want to upload files with Struts using the org.apache.struts.upload.FormFile but i would like to put a progress bar (like the gmail uploads forms or some like that), to upload the file (the files are big like 100Mb so the user must see what's going with the application). How i can do it?
Regards :)
You'll basically need to bring some shot of Ajax in so that the webbrowser and webserver can comminicate with each other asynchronously without the need to refresh/resubmit the page again and again. With Ajax the client will be able to poll the server for current progress at intervals and update the progress bar in the page accordingly.
Long story short, here's a nice blog article which covers this for the legacy Struts framework: http://kencochrane.blogspot.com/2006/03/ajax-struts-file-upload-progress-meter.html

asynchronous file upload with java servlet

Here's I want to do, I want to upload a file that will be processed by a servlet. I would use Apache Commons - File Upload to handle the file to be uploaded.
I've seen the gmail-like AJAX file upload, where there would be a hidden iframe that would later be populated with a javascript to stop showing the upload image or displaying a message that the upload is succesful. However, this uses PHP, where the php file to handle the file upload would include the javascript inside the iframe.
My question is, how would I do this in Java using servlets, without resorting to JSP and imitating the above implementation on PHP. I don't even know if this is possible, so please guide me on a good implementation (without external libraries except for commons fileupload).
Note: I am aware that there are libraries out there that could do this easily, but I first want to know how this happens, how this is possible, and to dirty my hands and learn this.
Edit: Just to add, I would use the streaming API of Apache-Commons FileUpload
It is exactly the same.
The client makes an HTTP request to the server (by submitting a form).
The server responds with some HTML (which links to or embeds some JavaScript).
Switching from PHP to Java is just a drop in replacement. You don't need to change any of the JavaScript. The user guide tells you how to set it up.
http://oreilly.com/pub/a/javascript/2002/02/08/iframe.html is the best idea to file-upload. i done file upload using hidden iframe. Please consult with attached link.

Categories

Resources