First download Apache Jakarta Commons FileUpload library from this link and add into classpath of your Application.
Validate HTTP Request
Now that you have installed the FileUpload library, you can start writing the code. First, we have to make sure the HTTP request is encoded in multipart format. This can be done using the static method isMultipartContent() of the ServletFileUpload class of the org.apache.commons.fileupload.servlet package:
// Parse the HTTP request…
}
In the above Java code snippet, request is a javax.servlet.http.HttpServletRequest object that encapsulates the HTTP request.
Parsing Form Data
Second, we will parse the form data contained in the HTTP request. Parsing the form data is very straightforward with the Apache Jakarta Commons FileUpload library:
List fileItemsList = servletFileUpload.parseRequest(request);
(In the above Java code snippet, DiskFileItemFactory is a class contained in the org.apache.commons.fileupload.disk package and List is an interface contained in the java.util package.)
If everything works fine, fileItemsList will contain a list of file items that are instances of FileItem of the org.apache.commons.fileupload package. A file item may contain an uploaded file or a simple name-value pair of a form field.
By default, the ServletFileUpload instance created by the above Java code uses the following values when parsing the HTTP request:
- Size threshold = 10,240 bytes. If the size of a file item is smaller than the size threshold, it will be stored in the memory. Otherwise it will be stored in a temporary file on disk.
- Maximum HTTP request body size = -1, which means the server will accept HTTP request bodies of any size.
- Repository = System default temp directory, whose value can be found by the Java code System.getProperty(“java.io.tmpdir”). Temporary files will be stored there.
If you do not like the default settings, you can change them using the methods setSizeThreshold() and setRespository() of the DiskFileItemFactory class and the setSizeMax() method of the ServletFileUpload class, like this:
diskFileItemFactory.setSizeThreshold(40960); /* the unit is bytes */
File repositoryPath = new File(“/temp”);
diskFileItemFactory.setRepository(repositoryPath);
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
servletFileUpload.setSizeMax(81920); /* the unit is bytes */
If the size of the HTTP request body exceeds the maximum you set, the SizeLimitExceededException exception (fully qualified name: org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) will be thrown when you call the parseRequest() method:
List fileItemsList = servletFileUpload.parseRequest(request);
/* Process file items… */
}
catch (SizeLimitExceededException ex) {
/* The size of the HTTP request body exceeds the limit */
}
Share or Bookmark This Post With :
Tomcat can be installed in two different ways and below is the information about how to increase or set the heap size for tomcat server in both scenarios -
1. Folder installation
– Create one new environment variable JAVA_OPTS
– Set following value -Xms256m -Xmx512m
– instead of 256m and 512m , you can enter the figure as per your requirement
2. Windows installation ( service)
– Start>Apache Tomcat 6>Configure Tomcat
– You will get following dialog box, click on ‘Java’ tab
– Here you can set ‘initial’ and ‘maximum’ memory pool size
After doing any of the above steps as per your installation, you can restart the server n it will start with above heap size.
Share or Bookmark This Post With :
Character Conversions from Browser to Database in JAVA
Characters in route to their final storage destination on the World Wide Web,
move through various layers of programming interfaces and can cross software and hardware boundaries.
This article provides helpful hints and best practices for accurately transporting character data from browser to
database and back again.
What if you want to POST non-ASCII data? All is well since you set that URIEncoding flag, right? Wrong. Tomcat doesn’t use the URIEncoding flag for POSTed form data. So, what does it use? ISO-8859-1.
So now, you’re back to where you started, and the simple application still greets Mr. ç°ä, instead of Mr. Japanese characters for Tanaka. Not good. Sun’s application server, however, does correctly
interpret both GET and POST data after setting the parameter-encoding tag as shown earlier, so this coding works well for users on that system.
Unfortunately, these solutions are completely server dependent, and you can’t always control where your applications will be deployed. Fortunately, server-independent solutions exist.
Perhaps the most basic server-independent solution is to set a context parameter indicating the character encoding choice for all forms in the application. Then your application can read the
context parameter and can set the request character encoding before reading any request parameters. You can set the request encoding in either a Java Servlet or in JSP syntax.
Setting the context parameter is done in the WEB-INF/web.xml file.
<param-name>PARAMETER_ENCODING</param-name>
<param-value>UTF-8</param-value>
</context-param>
Add the following code just before reading any parameters in the JSP file.
request.setCharacterEncoding(paramEncoding);
From a control servlet, you can read the parameter during servlet initiation and set the encoding before processing the parameters:
Share or Bookmark This Post With :
- dotProject – the Open Source Project Managem...
- The jQuery Form Plugin
- DocVerse has officially been acquired by Google
- HDGraph – a free tool for Windows to draw mu...
- RandomClass jQuery Plugin
- Paparazzi – a small utility for Mac OS X to ...
- Blocking SPAM using Postfix header_checks and Spam...
- Facebook, Paypal team up for virtual goods payment
- Opera Mini – the next generation mobile brow...
- XML/SWF Charts – powerful tool to create att...
- Send emails using PHPMailer an...
- Firefox Addons Essential for S...
- Free SEO Tools From SEOMoz.org...
- URL Rewriting for PHP Web Appl...
- Multiple Instances of Tomcat w...
- Apache 2.x + Tomcat 4.x + Load...
- Pagination in JSP/Java
- PHP – Mysql Open Source ...
- java.lang.OutOfMemoryError: Ja...
- HOW TO Subversion+Apache on Fe...





