Fastupload is the fastest form-based file upload java programming component so far. The component is based on RFC1867, written for java and j2ee development zone. It uses high performance byte-search algorithm to parse the multipart/form-data request, afterward save data within boundaries into the file system or save data into buffer in memory. Also, it provides a through resolution to resolve the encoding issue for text file uploading in multipart/form-data request. The component has the ability to filter off data within boundaries by content-type or file extension name specified in the processing of parse multipart/form-data headers.
Refer to RFC1867, High level API for multipart/form-data ServletInputStream
parsing. Sometime web site need to receives large size of uploading file, sometime web site need to receives a plenty of small size of uploading file in the high throughput. Fastupload provides two way to parse ServletInputStream
and very simple APIs for variant requirements from high performance web site.
The first way is read all bytes from ServletInputStream
into a buffer, then parse multipart data within two boundaries.
FastUploadParser
parse multipart/form-data input stream with the way in default. We recommend it. Here are a sample code snippet.
FastUploadParser fastUploadParser = new FastUploadParser(request);
List<MultiPart> list = fastUploadParser.parseList();
for (MultiPart e: list){
if (e.isFile()){
System.out.format("input field name: %s, file name:%s%n", e.getFieldName(), e.getFileName());
e.toFile( /file/ ) ; //write data to a file where you want to place
}
else {
System.out.format("input field name: %s, value:%s%n", e.getFieldName(), e.getString());
}
}
In the code snippet, after fastUploadParser.parseList()
execute, all data parts split by boundary are parsed an put into a list. {@link MultiPart} provides isFile()
method determine a multipart data whether is a uploading file or normal input. For example, in a form HTML code is. ie.
<input type="text" />
, regarding MultiPart
object is not a file, to a no-file type MultiPart
object, Fastupload limits to provides get content of it as String
<input type="file" />
, regardingMultiPart
object is a file, to a file typeMultiPart
object, Fastupload provides toFile(String)
method to write content of current multipart stream into a file. And getInputStream()
to open a inputream for current multipart stream. so APIs user can access it directly.The second way is read 8K bytes from ServletInputStream
to a buffer, then parse multipart data within two boundaries, create disk file for it and write content data in the temporary file when found a multipart data. read 8K bytes next until all byte of ServletInputStream
is read and parsed. Finally return a list of {@link MultiPart}. This way only need 8K buffer extra. It's very useful when receive a large size of file. Here are simple code snippet to show this usage way.
FileFactory fileFactory = FileFactory.getInstance();
fileFactory.setRepository(System.getProperty("user.home")+"/fastupload");
FastUploadParser fastUploadParser = new FastUploadParser(request, fileFactory);
List<MultiPart> list = fastUploadParser.parseList();
for (MultiPart e: list){
if (e.isFile()){
System.out.format("input field name: %s, file name:%s%n", e.getFieldName(), e.getFileName());
e.toFile( /target/ ) ; //move temporary file to where you want to place
}
else {
System.out.format("input field name: %s, value:%s%n", e.getFieldName(), e.getString());
}
}
As you see, most code is like the first way, the code snippet change the parse way. It set a temporary repository for FileFactory
object.
FileFactory fileFactory = FileFactory.getInstance();
fileFactory.setRepository(System.getProperty("user.home")+"/fastupload");
e.toFile( /target/ )
move the temporary file to the target place where you want because it isn't as same as first way that store multipart data in memory.
{@link FileFactory} controls parse way, give some advanced features as well.
FileFactory fileFactory = FileFactory.getInstance("UTF-8");
FileFactory fileFactory = FileFactory.getInstance("UTF-8");
fileFactory.setAllowedTypes("image/jpeg");
fileFactory.setAllowedExtensions(".jpg, .png");
FastUploadParser
provides content type and filefileFactory.setThreshold(200000);
limit parse content length of a part excludes headers, does not exceed the threshold. throw a runtime type of {@link ThresholdException} fileFactory.setMaxContentLength(2000000);
limit parse a content length of current multipart request, does not exceed the value. throw a runtime type of {@link ThresholdException}
ProgressListener listener = new ProgressListener(fastUploadParser);
listener.progress();
Tobe done..
you can add the dependency to your POM.xml, such that Maven resolve dependency automatically.
<dependency>
<groupId>net.sf.fastupload</groupId>
<artifactId>fastupload-core</artifactId>
<version>0.5.3</version>
</dependency>