📄 streaming.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FileUpload - The Streaming API</title>
<style type="text/css" media="all">
@import url("./css/maven-base.css");
@import url("./css/maven-theme.css");
@import url("./css/site.css");
</style>
<link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body class="composite">
<div id="banner">
<a href="../" id="bannerLeft">
<img src="../images/logo.png" alt="" />
</a>
<a href="index.html" id="bannerRight">
<img src="images/logo.png" alt="" />
</a>
<div class="clear">
<hr/>
</div>
</div>
<div id="breadcrumbs">
<div class="xleft">
Last Published: 18 January 2008
| Version: 1.2.1
</div>
<div class="xright"> <a href="http://www.apachecon.com/" class="externalLink">ApacheCon</a>
|
<a href="http://www.apache.org" class="externalLink">Apache</a>
|
<a href="../">Commons</a>
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="leftColumn">
<div id="navcolumn">
<h5>Commons FileUpload</h5>
<ul>
<li class="none">
<a href="index.html">Overview</a>
</li>
<li class="none">
<a href="using.html">User guide</a>
</li>
<li class="none">
<strong>Streaming API</strong>
</li>
<li class="none">
<a href="faq.html">FAQ</a>
</li>
<li class="none">
<a href="apidocs/index.html">Javadoc</a>
</li>
<li class="none">
<a href="mail-lists.html">Mailing lists</a>
</li>
<li class="none">
<a href="team-list.html">Team</a>
</li>
<li class="none">
<a href="tasks.html">Tasks</a>
</li>
<li class="none">
<a href="source-repository.html">SVN repository</a>
</li>
</ul>
<h5>Project Documentation</h5>
<ul>
<li class="collapsed">
<a href="project-info.html">Project Information</a>
</li>
<li class="collapsed">
<a href="project-reports.html">Project Reports</a>
</li>
</ul>
<h5>Commons</h5>
<ul>
<li class="none">
<a href="../">Home</a>
</li>
<li class="collapsed">
<a href="../components.html">Components</a>
</li>
<li class="collapsed">
<a href="../sandbox/index.html">Sandbox</a>
</li>
<li class="collapsed">
<a href="../dormant/index.html">Dormant</a>
</li>
<li class="none">
<a href="../volunteering.html">Volunteering</a>
</li>
<li class="none">
<a href="../patches.html">Contributing Patches</a>
</li>
<li class="none">
<a href="../building.html">Building Components</a>
</li>
<li class="none">
<a href="../releases/index.html">Releasing Components</a>
</li>
<li class="none">
<a href="http://wiki.apache.org/commons/FrontPage" class="externalLink">Wiki</a>
</li>
</ul>
<h5>ASF</h5>
<ul>
<li class="none">
<a href="http://www.apache.org/foundation/sponsorship.html" class="externalLink">Sponsorship</a>
</li>
<li class="none">
<a href="http://www.apache.org/foundation/thanks.html" class="externalLink">Thanks</a>
</li>
</ul>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img alt="Built by Maven" src="./images/logos/maven-feather.png"></img>
</a>
</div>
</div>
<div id="bodyColumn">
<div id="contentBox">
<div class="section"><h2><a name="Why_Streaming"></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 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 <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></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 requestboolean 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 handlerServletFileUpload upload = new ServletFileUpload();// Parse the requestFileItemIterator 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 ... }}</pre></div><p> That's all that's needed. Really! </p></div>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">©
2002-2008
The Apache Software Foundation
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -