Someone asked how simple is it to create a servlet using 3.0 technology which could upload a file. It took me about 5 minutes to do using NetBeans 7.2, and another 10 minutes to tweak for the file name. I hope that this proves how quick and easy it is to do. The code is also very readable.
Here is the code for my servlet, and form:
Wednesday, October 31, 2012
Subscribe to:
Post Comments
(
Atom
)
Popular Posts
-
Introduction This article is not another diatribe to tell you the importance of unit testing. I think we can all agree that it is important...
-
A friend of mine asked me if there was a list of reserved words in EL and JSF. He had previously looked for it, and after some Google search...
-
I saw a question posed on stackoverflow called Trouble with Primefaces 3.0.M2 SelectOneMenu Ajax behavior and I had just done an example a...
-
I was working on a couple of SSL based issues when I made a couple of observations. The default self-signed key generation in Java does not ...
-
This is an example on how to make a system call to the local operating system to execute external programs. This example was written to work...
-
We have been doing a lot of work lately with PrimeFaces. A common set of questions comes up about displaying <p:dialog/> boxes on a pa...
-
I was asked earlier today how to reset fields in a JSF application, if the validation fails. In his case, he had a Richfaces table which had...
-
Image by quasarkitten via Flickr The basics for creating a Maven archetype can be found in the Maven - Guide to Creating Archetypes . The ...
-
Previously, I posted an example of how to use JSF 1.2 with form based authentication (j_security_check). In this example, I use JSF 2.x to...
-
Abstract A common use case is to iterate over a collection of elements, and display them on a page. In the world of JSP, we would use a Ja...
8 comments :
Nice example. According path used in example, you are on unix based machine(Mac?) or linux. i am using win7 and i have sligthly changed code
@MultipartConfig(location="C:/temp")
works fine, except it breaks cyrylic filenames but it is a encoding issue.
thanks
You are correct sir! I am using a Mac. Thanks for the additional information for Windows users.
i wonder in case when the big files are uploaded, will the parts be available when all the content is parsed & saved/cache somewhere by the jvm or or they streamed ?
thanx
I have not tried it on any large files. There are usually practical limits to uploading files using an HTML uploader. If you have some large files to upload, I would be interested in seeing what difficulties if any that you encounter.
The example was not meant to be a complete solution. It was a simple demonstration of what is possible.
sorry not to be more specific in my question.
if there're 2 files in the form to be uploaded, will this line:
for (Part part : request.getParts()) ...
get hit right after the first part comes to server of after both files have already landed/transfered to the webserver ?
thanx.
@Kim From John's code, all the files are sent to the server at the same time and processed in the loop. So every file found in the multipart object is written to the server.
Also you can write individual files by doing
Part multipartfile = request.getPart("filename");
multipartfile.write(new File("/interesting/location/file"))
The code for uploading depends on the browser for uploads. Some allow multiple uploads, and some don't.
For some examples of file uploaders see my other posts.
http://javaevangelist.blogspot.com/2011/12/multiple-file-upload-examples.html
http://javaevangelist.blogspot.com/2010/12/multiple-file-upload-options.html
Thanks for adding information @Unekwu which is more clarifying.
Post a Comment