File: streaming.xml

package info (click to toggle)
libcommons-fileupload-java 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 752 kB
  • ctags: 654
  • sloc: java: 3,652; xml: 2,209; makefile: 13
file content (88 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<document>

  <properties>
    <title>The Streaming API</title>
  </properties>

<body>

  <section name="Why Streaming?">
    <p>
      The traditional API, which is described in the <a href="using.html">User
      Guide</a>, assumes, that file items must be stored somewhere, before
      they are actually accessable by the user. This approach is convenient,
      because it allows easy access to an items contents. On the other hand,
      it is memory and time consuming.
    </p>
    <p>
      The streaming API allows you to trade a little bit of convenience for
      optimal performance and and a low memory profile. Additionally, the
      API is more lightweight, thus easier to understand.
    </p>
  </section>

  <section name="How it works">
    <p>
      Again, the <code>FileUpload</code> class is used for accessing the
      form fields and fields in the order, in which they have been sent
      by the client. However, the <code>FileItemFactory</code> is completely
      ignored.
    </p>
  </section>

  <section name="Parsing the request">
    <p>
      First of all, do not forget to ensure, that a request actually is a
      a file upload request. This is typically done using the same static
      method, which you already know from the traditional API.
    </p>
<source><![CDATA[// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);]]></source>
    <p>
      Now we are ready to parse the request into its constituent items. Here's
      how we do it:
    </p>
<source><![CDATA[
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();

// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
    FileItemStream item = iter.next();
    String name = item.getFieldName();
    InputStream stream = item.openStream();
    if (item.isFormField()) {
        System.out.println("Form field " + name + " with value "
            + Streams.asString(stream) + " detected.");
    } else {
        System.out.println("File field " + name + " with file name "
            + item.getName() + " detected.");
        // Process the input stream
        ...
    }
}]]></source>
    <p>
      That's all that's needed. Really!
    </p>
  </section>
</body>
</document>