Friday, December 24, 2010

Multiple File Upload Options

I was recently confronted with a question about how to do multiple file uploads from a browser based application. The HTML 4.x specification is pretty open on the file upload requirements, and all of the browser implementations are very basic.  Firefox, Safari, Web Toolkit, and Internet Explorer all provide a method to upload one file at a time, but no multiple file upload. As a result, clever coders have come up with a number of solutions. Here are a few.


These are some approaches available to you. If you are using a JSF based framework, a number of them already include a multiple file upload component based on Apache Commons File Upload.

Wednesday, December 08, 2010

Apache HTTPClient 4.x Preemptive Authentication

I am using the Apache HttpClient for some work I am doing at the office. It is a really cool utility to perform web based work. A common requirement of such work is to use authentication.

The default behavior of the HttpClient is to try and connect to the resource and read the response. If the response is a 401 Unauthorized, the client sends the request again using the credentials that are set in the client. This results in an unnecessary double posting of the request. It is however compliant with RFC2616 Section 10.4.2 which describes this behavior.

In the previous version of the client (3.x), you could set the preemptive authentication with the code below:


httpClient.getParams().setAuthenticationPreemptive(true);

However, version 4.x does not support this convenient arrangement. There is a more flexible arrangement using interceptors. The client supports interceptors on both the request, and the response. The use of interceptors makes the code a little more complex, but the flexibility of using multiple interceptors, and ordering them makes up for the additional code required.

I can not provide an example of my code since it was something I created for work, but here are two examples. The first is complete, and the second is simply an example.



There is an additional post on stackoverflow which indicates you may be able to use a simple addition to the header. This may work for simple clients, but it is not as flexible as the interceptor form. I have not tested the code, but have included Adam Batkin's code snippet below.


String username = ...
String password = ...
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);

HttpRequest request = ...
request.addHeader(new BasicScheme().authenticate(creds, request));

Apache Commons IO 2.0

Congrats to the Apache Commons IO Team on the release of the 2.0 framework. It has been a long time coming. The latest libraries require Java 1.5 which is a quantum jump in Apache terms. The old library required Java 1.3.

The IOUtils class provides a number of convenience methods to handle most IO operations. One of my favorite methods is IOUtils.copy(InputStream input, OutputStream output). A simple method, but so very useful since it is so common.

Have you ever needed to create unit tests on methods which require a closed InputStream, or OutputStream. I am sure you have, if not you just have not gotten there yet. Apache Commons IO offers you ClosedInputStream and ClosedOutputStream.

There are some other nice classes especially for debugging like CountingInputStream and CountingOutputStream which records the number of bytes read, or written. I can see another use for cloud computing models where you are charged for data transfers. This can help you keep track of those numbers for comparison with your charges.

If you have not taken the time to look at these great tools, take a couple of minutes to just look at the javadocs. I am sure you will be impressed like me.
Enhanced by Zemanta

Popular Posts