315 lines
13 KiB
HTML
315 lines
13 KiB
HTML
<!DOCTYPE html>
|
|
<!--
|
|
| Generated by Apache Maven Doxia at 24 December 2018
|
|
| Rendered using Apache Maven Fluido Skin 1.3.0
|
|
-->
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
<head>
|
|
<meta charset="iso-8859-1" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<meta name="Date-Revision-yyyymmdd" content="20181224" />
|
|
<meta http-equiv="Content-Language" content="en" />
|
|
<title>FileUpload – The Streaming API</title>
|
|
|
|
<link rel="stylesheet" href="./css/bootstrap.min.css" type="text/css" />
|
|
<link rel="stylesheet" href="./css/site.css" type="text/css" />
|
|
<link rel="stylesheet" href="./css/print.css" media="print" />
|
|
|
|
<script type="text/javascript" src="./js/jquery.min.js"></script>
|
|
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
|
|
<script type="text/javascript" src="./js/prettify.min.js"></script>
|
|
<script type="text/javascript" src="./js/site.js"></script>
|
|
|
|
|
|
</head>
|
|
|
|
<body class="composite">
|
|
<a href="http://commons.apache.org/" id="bannerLeft" title="Apache Commons logo">
|
|
<img class="logo-left" src="./images/commons-logo.png" alt="Apache Commons logo"/>
|
|
</a>
|
|
<a href="index.html" id="bannerRight">
|
|
<img class="logo-right" src="images/logo.png" alt="Commons FileUpload"/>
|
|
</a>
|
|
<div class="clear"></div>
|
|
|
|
<div class="navbar">
|
|
<div class="navbar-inner">
|
|
<div class="container-fluid">
|
|
<a class="brand" href="http://commons.apache.org/proper/commons-fileupload/">Apache Commons FileUpload ™</a>
|
|
<ul class="nav">
|
|
|
|
<li id="publishDate">Last Published: 24 December 2018</li>
|
|
<li class="divider">|</li> <li id="projectVersion">Version: 1.4</li>
|
|
</ul>
|
|
<div class="pull-right"> <ul class="nav">
|
|
<li>
|
|
<a href="http://www.apachecon.com/" class="externalLink" title="ApacheCon">
|
|
ApacheCon</a>
|
|
</li>
|
|
<li>
|
|
<a href="http://www.apache.org" class="externalLink" title="Apache">
|
|
Apache</a>
|
|
</li>
|
|
<li>
|
|
<a href="../../" title="Commons">
|
|
Commons</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="container-fluid">
|
|
<table class="layout-table">
|
|
<tr>
|
|
<td class="sidebar">
|
|
<div class="well sidebar-nav">
|
|
<ul class="nav nav-list">
|
|
<li class="nav-header">Commons FileUpload</li>
|
|
<li class="none">
|
|
<a href="index.html" title="Overview">
|
|
Overview</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="using.html" title="User guide">
|
|
User guide</a>
|
|
</li>
|
|
<li class="none active">
|
|
<a href="streaming.html" title="Streaming API">
|
|
Streaming API</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="faq.html" title="FAQ">
|
|
FAQ</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="javadocs/api-release/index.html" title="Javadoc (Latest release)">
|
|
Javadoc (Latest release)</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="download_fileupload.cgi" title="Download">
|
|
Download</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="security-reports.html" title="Security Reports">
|
|
Security Reports</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="mail-lists.html" title="Mailing lists">
|
|
Mailing lists</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="issue-tracking.html" title="Issue Tracking">
|
|
Issue Tracking</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="team-list.html" title="Team">
|
|
Team</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="source-repository.html" title="Source repository">
|
|
Source repository</a>
|
|
</li>
|
|
</ul>
|
|
<ul class="nav nav-list">
|
|
<li class="nav-header"><i class="icon-info-sign"></i>Project Documentation</li>
|
|
<li class="collapsed">
|
|
<a href="project-info.html" title="Project Information">
|
|
Project Information</a>
|
|
</li>
|
|
<li class="collapsed">
|
|
<a href="project-reports.html" title="Project Reports">
|
|
Project Reports</a>
|
|
</li>
|
|
</ul>
|
|
<ul class="nav nav-list">
|
|
<li class="nav-header">Commons</li>
|
|
<li class="none">
|
|
<a href="../../" title="Home">
|
|
Home</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="http://www.apache.org/licenses/" class="externalLink" title="License">
|
|
License</a>
|
|
</li>
|
|
<li class="collapsed">
|
|
<a href="../../components.html" title="Components">
|
|
Components</a>
|
|
</li>
|
|
<li class="collapsed">
|
|
<a href="../../sandbox/index.html" title="Sandbox">
|
|
Sandbox</a>
|
|
</li>
|
|
<li class="collapsed">
|
|
<a href="../../dormant/index.html" title="Dormant">
|
|
Dormant</a>
|
|
</li>
|
|
</ul>
|
|
<ul class="nav nav-list">
|
|
<li class="nav-header">General Information</li>
|
|
<li class="none">
|
|
<a href="../../security.html" title="Security">
|
|
Security</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../../volunteering.html" title="Volunteering">
|
|
Volunteering</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../../patches.html" title="Contributing Patches">
|
|
Contributing Patches</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../../building.html" title="Building Components">
|
|
Building Components</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../../commons-parent-pom.html" title="Commons Parent Pom">
|
|
Commons Parent Pom</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../../build-plugin/index.html" title="Commons Build Plugin">
|
|
Commons Build Plugin</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="../../releases/index.html" title="Releasing Components">
|
|
Releasing Components</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="http://wiki.apache.org/commons/FrontPage" class="externalLink" title="Wiki">
|
|
Wiki</a>
|
|
</li>
|
|
</ul>
|
|
<ul class="nav nav-list">
|
|
<li class="nav-header">ASF</li>
|
|
<li class="none">
|
|
<a href="http://www.apache.org/foundation/how-it-works.html" class="externalLink" title="How the ASF works">
|
|
How the ASF works</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="http://www.apache.org/foundation/getinvolved.html" class="externalLink" title="Get Involved">
|
|
Get Involved</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="http://www.apache.org/dev/" class="externalLink" title="Developer Resources">
|
|
Developer Resources</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="http://www.apache.org/foundation/policies/conduct.html" class="externalLink" title="Code of Conduct">
|
|
Code of Conduct</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="http://www.apache.org/foundation/sponsorship.html" class="externalLink" title="Sponsorship">
|
|
Sponsorship</a>
|
|
</li>
|
|
<li class="none">
|
|
<a href="http://www.apache.org/foundation/thanks.html" class="externalLink" title="Thanks">
|
|
Thanks</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<div id="poweredBy">
|
|
<a href="http://www.apache.org/events/current-event.html" title="ApacheCon" class="builtBy">
|
|
<img class="builtBy" alt="ApacheCon" src="http://www.apache.org/events/current-event-125x125.png" />
|
|
</a>
|
|
<a href="http://maven.apache.org/" title="Maven" class="builtBy">
|
|
<img class="builtBy" alt="Maven" src="http://maven.apache.org/images/logos/maven-feather.png" />
|
|
</a>
|
|
</div>
|
|
</td>
|
|
<td class="content">
|
|
|
|
|
|
|
|
<div class="section">
|
|
<h2><a name="Why_Streaming.3F"></a>Why Streaming?</h2>
|
|
|
|
<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 a low memory profile. Additionally, the
|
|
API is more lightweight, thus easier to understand.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="section">
|
|
<h2><a name="How_it_works"></a>How it works</h2>
|
|
|
|
<p>
|
|
Again, the <tt>FileUpload</tt> class is used for accessing the
|
|
form fields and fields in the order in which they have been sent
|
|
by the client. However, the <tt>FileItemFactory</tt> is completely
|
|
ignored.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="section">
|
|
<h2><a name="Parsing_the_request"></a>Parsing the request</h2>
|
|
|
|
<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>
|
|
|
|
<div class="source">
|
|
<pre>// Check that we have a file upload request
|
|
boolean isMultipart = ServletFileUpload.isMultipartContent(request);</pre></div>
|
|
|
|
<p>
|
|
Now we are ready to parse the request into its constituent items. Here's
|
|
how we do it:
|
|
</p>
|
|
|
|
<div class="source">
|
|
<pre>// 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.getInputStream();
|
|
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
|
|
...
|
|
}
|
|
}</pre></div>
|
|
|
|
<p>
|
|
That's all that's needed. Really!
|
|
</p>
|
|
</div>
|
|
|
|
|
|
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="footer">
|
|
<p>Copyright © 2002-2018
|
|
<a href="https://www.apache.org/">The Apache Software Foundation</a>.
|
|
All Rights Reserved.</p>
|
|
</div>
|
|
</body>
|
|
|
|
</html>
|