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

📄 using.html

📁 最好的java上传组件
💻 HTML
📖 第 1 页 / 共 2 页
字号:
// Parse the requestList /* FileItem */ items = upload.parseRequest(request);</pre></div>        <p>        Of course, each of the configuration methods is independent of the        others, but if you want to configure the factory all at once, you can        do that with an alternative constructor, like this:      </p>    <div class="source"><pre>// Create a factory for disk-based file itemsDiskFileItemFactory factory = new DiskFileItemFactory(        yourMaxMemorySize, yourTempDirectory);</pre></div>        <p>        Should you need further control over the parsing of the request, such        as storing the items elsewhere - for example, in a database - you will        need to look into <a href="customizing.html">customizing</a> FileUpload.      </p>    </div>  </div><div class="section"><a name="Processing_the_uploaded_items"></a><h2>Processing the uploaded items</h2>    <p>      Once the parse has completed, you will have a <code>List</code> of file      items that you need to process. In most cases, you will want to handle      file uploads differently from regular form fields, so you might process      the list like this:    </p>    <div class="source"><pre>// Process the uploaded itemsIterator iter = items.iterator();while (iter.hasNext()) {    FileItem item = (FileItem) iter.next();    if (item.isFormField()) {        processFormField(item);    } else {        processUploadedFile(item);    }}</pre></div>      <p>      For a regular form field, you will most likely be interested only in the      name of the item, and its <code>String</code> value. As you might expect,      accessing these is very simple.    </p>    <div class="source"><pre>// Process a regular form fieldif (item.isFormField()) {    String name = item.getFieldName();    String value = item.getString();    ...}</pre></div>      <p>      For a file upload, there are several different things you might want to      know before you process the content. Here is an example of some of the      methods you might be interested in.    </p>    <div class="source"><pre>// Process a file uploadif (!item.isFormField()) {    String fieldName = item.getFieldName();    String fileName = item.getName();    String contentType = item.getContentType();    boolean isInMemory = item.isInMemory();    long sizeInBytes = item.getSize();    ...}</pre></div>      <p>      With uploaded files, you generally will not want to access them via      memory, unless they are small, or unless you have no other alternative.      Rather, you will want to process the content as a stream, or write the      entire file to its ultimate location. FileUpload provides simple means of      accomplishing both of these.    </p>    <div class="source"><pre>// Process a file uploadif (writeToFile) {    File uploadedFile = new File(...);    item.write(uploadedFile);} else {    InputStream uploadedStream = item.getInputStream();    ...    uploadedStream.close();}</pre></div>      <p>      Note that, in the default implementation of FileUpload, <code>write()</code>      will attempt to rename the file to the specified destination, if the data      is already in a temporary file. Actually copying the data is only done if      the the rename fails, for some reason, or if the data was in memory.    </p>    <p>      If you do need to access the uploaded data in memory, you need simply      call the <code>get()</code> method to obtain the data as an array of      bytes.    </p>    <div class="source"><pre>// Process a file upload in memorybyte[] data = item.get();...</pre></div>    </div><div class="section"><a name="Interaction_with_virus_scanners"></a><h2>Interaction with virus scanners</h2>    <p>      Virus scanners running on the same system as the web container can cause      some unexpected behaviours for applications using FileUpload. This section      describes some of the behaviours that you might encounter, and provides      some ideas for how to handle them.    </p>    <p>      The default implementation of FileUpload will cause uploaded items above      a certain size threshold to be written to disk. As soon as such a file is      closed, any virus scanner on the system will wake up and inspect it, and      potentially quarantine the file - that is, move it to a special location      where it will not cause problems. This, of course, will be a surprise to      the application developer, since the uploaded file item will no longer be      available for processing. On the other hand, uploaded items below that      same threshold will be held in memory, and therefore will not be seen by      virus scanners. This allows for the possibility of a virus being retained      in some form (although if it is ever written to disk, the virus scanner      would locate and inspect it).    </p>    <p>      One commonly used solution is to set aside one directory on the system      into which all uploaded files will be placed, and to configure the virus      scanner to ignore that directory. This ensures that files will not be      ripped out from under the application, but then leaves responsibility for      virus scanning up to the application developer. Scanning the uploaded      files for viruses can then be performed by an external process, which      might move clean or cleaned files to an "approved" location, or by      integrating a virus scanner within the application itself. The details of      configuring an external process or integrating virus scanning into an      application are outside the scope of this document.    </p>  </div><div class="section"><a name="What_s_next"></a><h2>What's next</h2>    <p>      Hopefully this page has provided you with a good idea of how to use      FileUpload in your own applications. For more detail on the methods      introduced here, as well as other available methods, you should refer      to the <a href="apidocs/index.html">JavaDocs</a>.    </p>    <p>      The usage described here should satisfy a large majority of file upload      needs. However, should you have more complex requirements, FileUpload      should still be able to help you, with it's flexible      <a href="customizing.html">customization</a> capabilities.    </p>  </div></div></div><div class="clear"><hr></hr></div><div id="footer"><div class="xright">

⌨️ 快捷键说明

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