Menu

Fastupload Home

Link Qian

Introduction

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.

Features

  • support multiple boundaries
  • support parsing sub-boundary
  • supply resolution for uni-code text through
  • support content type and file extension name filter
  • support limitation of uploading file size, and progress during the uploading
  • faster performance, low memory occupy and high stability
  • support struts2

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.

Basic Usage

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" />, regarding
    MultiPart object is a file, to a file type
    MultiPart 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.

    Advanced APIs

    {@link FileFactory} controls parse way, give some advanced features as well.

  • FileFactory fileFactory = FileFactory.getInstance("UTF-8");

  • create a {@link FileFactory} instance with specific charset name. Content of a multipart will convert with the charset name when find the content type is text. Another thing is it converts the file name of content header with the charset when find it. It is a key feature of Fastupload open-source project.


  • FileFactory fileFactory = FileFactory.getInstance("UTF-8");
    fileFactory.setAllowedTypes("image/jpeg");
    fileFactory.setAllowedExtensions(".jpg, .png");
  • FastUploadParser provides content type and file
    name filter function. Parser ignore a multipart content when found its
    content header does not match AllowedTypes or extension name of
    filename entity does not match AllowedExtensions.

  • fileFactory.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}

  • Setup a {@link ProgressListener} to know parsing progress
  • ProgressListener listener = new ProgressListener(fastUploadParser);
    listener.progress();

    Charset Encoding

    Tobe done..

    Maven Coordinate

    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>


    Want the latest updates on software, tech news, and AI?
    Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.