⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 using.html

📁 最好的java上传组件
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>Using FileUpload - FileUpload</title><style type="text/css" media="all">          @import url("./style/maven-base.css");                    @import url("./style/maven-theme.css");@import url("./style/project.css");</style><link rel="stylesheet" href="./style/print.css" type="text/css" media="print"></link><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></meta><meta name="author" content="Martin Cooper"></meta><meta name="email" content="martinc@apache.org"></meta></head><body class="composite"><div id="banner"><a href="http://jakarta.apache.org" id="organizationLogo"><img alt="The Apache Software Foundation" src="http://jakarta.apache.org/images/original-jakarta-logo.gif"></img></a><a href="http://jakarta.apache.org/commons/fileupload/" id="projectLogo"><img alt="FileUpload" src="./images/logo.png"></img></a><div class="clear"><hr></hr></div></div><div id="breadcrumbs"><div class="xleft">Last published: 08 June 2006                <span class="separator">|</span>燚oc for  1.1.1                </div><div class="xright">                <a href="http://jakarta.apache.org/commons/" class="externalLink" title="External Link">Jakarta Commons</a>      </div><div class="clear"><hr></hr></div></div><div id="leftColumn"><div id="navcolumn"><div id="menuCommons_FileUpload"><h5>Commons FileUpload</h5><ul><li class="none"><a href="index.html">Overview</a></li><li class="none"><strong><a href="using.html">User guide</a></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="cvs-usage.html">SVN repository</a></li></ul></div><div id="menuProject_Documentation"><h5>Project Documentation</h5><ul><li class="none"><a href="index.html">About</a></li><li class="collapsed"><a href="project-info.html">Project Info</a></li><li class="collapsed"><a href="maven-reports.html">Project Reports</a></li><li class="none"><a href="http://jakarta.apache.org/commons/charter.html" class="externalLink" title="External Link">Development Process</a></li></ul></div><div id="menuCommons"><h5>Commons</h5><ul><li class="none"><a href="http://jakarta.apache.org/commons/" class="externalLink" title="External Link">Home</a></li><li class="collapsed"><a href="http://jakarta.apache.org/commons/components.html" class="externalLink" title="External Link">Components</a></li><li class="collapsed"><a href="http://jakarta.apache.org/commons/sandbox/index.html" class="externalLink" title="External Link">Sandbox</a></li><li class="collapsed"><a href="http://jakarta.apache.org/commons/dormant/index.html" class="externalLink" title="External Link">Dormant</a></li><li class="none"><a href="http://jakarta.apache.org/commons/volunteering.html" class="externalLink" title="External Link">Volunteering</a></li><li class="none"><a href="http://jakarta.apache.org/commons/patches.html" class="externalLink" title="External Link">Contributing Patches</a></li><li class="none"><a href="http://jakarta.apache.org/commons/building.html" class="externalLink" title="External Link">Building Components</a></li><li class="none"><a href="http://jakarta.apache.org/commons/releases/index.html" class="externalLink" title="External Link">Releasing Components</a></li><li class="none"><a href="http://wiki.apache.org/jakarta-commons/FrontPage" class="externalLink" title="External Link">Wiki</a></li></ul></div><div id="legend"><h5>Legend</h5><ul><li class="externalLink">External Link</li><li class="newWindow">Opens in a new window</li></ul></div><a href="http://maven.apache.org/" title="Built by Maven" id="poweredBy"><img alt="Built by Maven" src="./images/logos/maven-feather.png"></img></a></div></div><div id="bodyColumn"><div class="contentBox"><div class="section"><a name="Using_FileUpload"></a><h2>Using FileUpload</h2>    <p>      FileUpload can be used in a number of different ways, depending upon the      requirements of your application. In the simplest case, you will call a      single method to parse the servlet request, and then process the list of      items as they apply to your application. At the other end of the scale,      you might decide to customize FileUpload to take full control of the way      in which individual items are stored; for example, you might decide to      stream the content into a database.    </p>    <p>      Here, we will describe the basic principles of FileUpload, and illustrate      some of the simpler - and most common - usage patterns. Customization of      FileUpload is described <a href="customizing.html">elsewhere</a>.    </p>  </div><div class="section"><a name="How_it_works"></a><h2>How it works</h2>    <p>      A file upload request comprises an ordered list of <em>items</em> that      are encoded according to      <a href="http://www.ietf.org/rfc/rfc1867.txt" class="externalLink" title="External Link">RFC 1867</a>,      "Form-based File Upload in HTML". FileUpload can parse such a request      and provide your application with a list of the individual uploaded      items. Each such item implements the <code>FileItem</code> interface,      regardless of its underlying implementation.    </p>    <p>      Each file item has a number of properties that might be of interest for      your application. For example, every item has a name and a content type,      and can provide an <code>InputStream</code> to access its data. On the      other hand, you may need to process items differently, depending upon      whether the item is a regular form field - that is, the data came from      an ordinary text box or similar HTML field - or an uploaded file. The      <code>FileItem</code> interface provides the methods to make such a      determination, and to access the data in the most appropriate manner.    </p>    <p>      FileUpload creates new file items using a <code>FileItemFactory</code>.      This is what gives FileUpload most of its flexibility. The factory has      ultimate control over how each item is created. The factory implementation      that currently ships with FileUpload stores the item's data in memory or      on disk, depending on the size of the item (i.e. bytes of data). However,      this behavior can be customized to suit your application.    </p>  </div><div class="section"><a name="Servlets_and_Portlets"></a><h2>Servlets and Portlets</h2>    <p>      Starting with version 1.1, FileUpload supports file upload requests in      both servlet and portlet environments. The usage is almost identical in      the two environments, so the remainder of this document refers only to      the servlet environment.    </p>    <p>      If you are building a portlet application, the following are the two      distinctions you should make as you read this document:      <ul>        <li>          Where you see references to the <code>ServletFileUpload</code> class,          substitute the <code>PortletFileUpload</code> class.        </li>        <li>          Where you see references to the <code>HttpServletRequest</code> class,          substitute the <code>ActionRequest</code> class.        </li>      </ul>    </p>  </div><div class="section"><a name="Parsing_the_request"></a><h2>Parsing the request</h2>    <p>      Before you can work with the uploaded items, of course, you need to parse      the request itself. Ensuring that the request is actually a file upload      request is straightforward, but FileUpload makes it simplicity itself, by      providing a static method to do just that.    </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.      </p>    <div class="subsection"><a name="The_simplest_case"></a><h3>The simplest case</h3>      <p>        The simplest usage scenario is the following:        <ul>          <li>            Uploaded items should be retained in memory as long as they are            reasonably small.          </li>          <li>            Larger items should be written to a temporary file on disk.          </li>          <li>            Very large upload requests should not be permitted.          </li>          <li>            The built-in defaults for the maximum size of an item to            be retained in memory, the maximum permitted size of an upload            request, and the location of temporary files are acceptable.          </li>        </ul>      </p>      <p>        Handling a request in this scenario couldn't be much simpler:      </p>    <div class="source"><pre>// Create a factory for disk-based file itemsFileItemFactory factory = new DiskFileItemFactory();// Create a new file upload handlerServletFileUpload upload = new ServletFileUpload(factory);// Parse the requestList /* FileItem */ items = upload.parseRequest(request);</pre></div>        <p>        That's all that's needed. Really!      </p>      <p>        The result of the parse is a <code>List</code> of file items, each of        which implements the <code>FileItem</code> interface. Processing these        items is discussed below.      </p>    </div>    <div class="subsection"><a name="Exercising_more_control"></a><h3>Exercising more control</h3>       <p>        If your usage scenario is close to the simplest case, described above,        but you need a little more control, you can easily customize the        behavior of the upload handler or the file item factory or both. The        following example shows several configuration options:      </p>    <div class="source"><pre>// Create a factory for disk-based file itemsDiskFileItemFactory factory = new DiskFileItemFactory();// Set factory constraintsfactory.setSizeThreshold(yourMaxMemorySize);factory.setRepository(yourTempDirectory);// Create a new file upload handlerServletFileUpload upload = new ServletFileUpload(factory);// Set overall request size constraintupload.setSizeMax(yourMaxRequestSize);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -