commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rademacher Tobias <>
Subject AW: [FileUpload] Speeding up uploads
Date Thu, 12 Jun 2003 08:05:35 GMT
Dear Martin,

> > Wouldn't NIO come with some performance improvements 
> especially for large
> > files?
> I don't know a whole lot about NIO, but I doubt that it would make any
> difference. Since a given request is guaranteed to be 
> serviced entirely by a
> single thread, there's not much opportunity for getting 
> clever. But then
> again, even if there was, that's part of the container, and 
> not something
> Commons FileUpload could do anything about.

As NIO works directly on OS Pipes I used it in order to get around on the
byte buffer
in-VM memory problem. It's fairly easy to wrap ServletOutputStream with NIO

	 * Writes the file from server to client using NIO over HTTP.
	 * @param out stream from tomcat to client browser
	 * @param fileName the file to write over the wire
	 * @exception FileNotFoundException if the request file does not
	 * @exception IOException if the sending process fails
	private void sendFile(ServletOutputStream out, String fileName)
		throws FileNotFoundException, IOException {

		RandomAccessFile file = null;
		FileChannel fileChannel = null;
		WritableByteChannel servletOutChannel = null;
		MappedByteBuffer buffer = null;

		try {
			file = new RandomAccessFile(fileName, "r");
			fileChannel = file.getChannel();

			servletOutChannel = Channels.newChannel(out);

			LOGGER.debug("Begin to write out:" +

			fileChannel.transferTo(0L, fileChannel.size(),

		} finally {
			if (buffer != null)
				buffer = null;
			if (fileChannel != null)
			if (servletOutChannel != null)
			if (file != null)

Using this approach vice versa (on InputStreams may) may work as well into a
MulitPartStream using 
java.nio.ByteBuffer and Channels to transfer byte bulks. (Unfortunatly this
works only on
1.4+ which is not very portable.) I actually begin porting your module to
NIO but as I don't
understand _really_ what going on there (not the NIO stuff but the RFC is
not really a good guide) so if you interested into that clumsy code, fell
free to ask me for a copy. The main benifit of NIO for Upload would be that
you don't need to configure a max file size any more due to complete
transfer could be managed outside the JVM head memory.


View raw message