Servlet技术出现以前,文件上传的编程仍然是一项很困难的任务,它涉及在服务器端解析原始的HTTP响应。为了减轻编程的痛苦,开发人员借助于商业的文件上传组件。值得庆幸的是,2003年,Apache Software Foundation发布了开源的Commons FileUpload组件,它很快成为了Java Web应用程序员的利器。

经过很多年,Servlet的设计人员才意识到文件文件上传的重要性,并终于成为Servlet 3.0的内置特性。Servlet 3.0的开发人员不再需要将Commons FileUpload组件导入到他们的项目中去。

为此,在Spring MVC中处理文件上传有两种情况:

  • 在Servlet 3.0版本以下,使用Apache Commons FileUpload组件;
  • 在Servlet 30.版本以上,利用Servlet 3.0及其更高版本的内置支持。

无论使用哪个版本的Servlet,都要利用相同的API来处理已经上传的文件。本篇博客将会介绍如何在需要支持文件上传的Spring MVC应用中使用Commons FileUpload和Servlet 3.0文件上传特性。

一 前端编程

为了上传文件,必须将HTML表格enctype属性值设置为multipart/form-data,像下面这样:

<form  action="action" method="post" enctype="multipart/form-data">
   select a file <input type="file" name="fieldName"/>
   <input type="submit" value="Upload"/>
</form>

表格中必须包含类型为file的一个input元素,它会显示成一个按钮,单击时,它会打开一个对话框,用来选择文件。

在HTML 5之前,如果想要上传多个文件,必须使用多个类型为file的input元素。但是在HTML 5中,通过在input元素中引入multiple属性,使得多个文件的上传变得更加简单。在HTML 5中编写以下任意一行代码,便可以生成一个按钮来选择多个文件:

<input type="file" name="fieldName" multiple/>
<input type="file" name="fieldName" multiple="multiple"/>
<input type="file" name="fieldName" multiple=""/>

二 MultipartFile接口

在Spring MVC中处理已经上传的文件十分容易。上传到Spring MVC应用程序中的文件会被包含在一个MultipartFile对象中。我们唯一的任务就是,用类型MultipartFile的属性编写一个domain类。

org.springframework.web.multipart.MultipartFile接口源代码如下:

/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed 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
 *
 *      https://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.
 */

package org.springframework.web.multipart;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.FileCopyUtils;

/**
 * A representation of an uploaded file received in a multipart request.
 *
 * <p>The file contents are either stored in memory or temporarily on disk.
 * In either case, the user is responsible for copying file contents to a
 * session-level or persistent store as and if desired. The temporary storage
 * will be cleared at the end of request processing.
 *
 * @author Juergen Hoeller
 * @author Trevor D. Cook
 * @since 29.09.2003
 * @see org.springframework.web.multipart.MultipartHttpServletRequest
 * @see org.springframework.web.multipart.MultipartResolver
 */
public interface MultipartFile extends InputStreamSource {

    /**
     * Return the name of the parameter in the multipart form.
     * @return the name of the parameter (never {@code null} or empty)
     */
    String getName();

    /**
     * Return the original filename in the client's filesystem.
     * <p>This may contain path information depending on the browser used,
     * but it typically will not with any other than Opera.
     * @return the original filename, or the empty String if no file has been chosen
     * in the multipart form, or {@code null} if not defined or not available
     * @see org.apache.commons.fileupload.FileItem#getName()
     * @see org.springframework.web.multipart.commons.CommonsMultipartFile#setPreserveFilename
     */
    @Nullable
    String getOriginalFilename();

    /**
     * Return the content type of the file.
     * @return the content type, or {@code null} if not defined
     * (or no file has been chosen in the multipart form)
     */
    @Nullable
    String getContentType();

    /**
     * Return whether the uploaded file is empty, that is, either no file has
     * been chosen in the multipart form or the chosen file has no content.
     */
    boolean isEmpty();

    /**
     * Return the size of the file in bytes.
     * @return the size of the file, or 0 if empty
     */
    long getSize();

    /**
     * Return the contents of the file as an array of bytes.
     * @return the contents of the file as bytes, or an empty byte array if empty
     * @throws IOException in case of access errors (if the temporary store fails)
     */
    byte[] getBytes() throws IOException;

    /**
     * Return an InputStream to read the contents of the file from.
     * <p>The user is responsible for closing the returned stream.
     * @return the contents of the file as stream, or an empty stream if empty
     * @throws IOException in case of access errors (if the temporary store fails)
     */
    @Override
    InputStream getInputStream() throws IOException;

    /**
     * Return a Resource representation of this MultipartFile. This can be used
     * as input to the {@code RestTemplate} or the {@code WebClient} to expose
     * content length and the filename along with the InputStream.
     * @return this MultipartFile adapted to the Resource contract
     * @since 5.1
     */
    default Resource getResource() {
        return new MultipartFileResource(this);
    }

    /**
     * Transfer the received file to the given destination file.
     * <p>This may either move the file in the filesystem, copy the file in the
     * filesystem, or save memory-held contents to the destination file. If the
     * destination file already exists, it will be deleted first.
     * <p>If the target file has been moved in the filesystem, this operation
     * cannot be invoked again afterwards. Therefore, call this method just once
     * in order to work with any storage mechanism.
     * <p><b>NOTE:</b> Depending on the underlying provider, temporary storage
     * may be container-dependent, including the base directory for relative
     * destinations specified here (e.g. with Servlet 3.0 multipart handling).
     * For absolute destinations, the target file may get renamed/moved from its
     * temporary location or newly copied, even if a temporary copy already exists.
     * @param dest the destination file (typically absolute)
     * @throws IOException in case of reading or writing errors
     * @throws IllegalStateException if the file has already been moved
     * in the filesystem and is not available anymore for another transfer
     * @see org.apache.commons.fileupload.FileItem#write(File)
     * @see javax.servlet.http.Part#write(String)
     */
    void transferTo(File dest) throws IOException, IllegalStateException;

    /**
     * Transfer the received file to the given destination file.
     * <p>The default implementation simply copies the file input stream.
     * @since 5.1
     * @see #getInputStream()
     * @see #transferTo(File)
      */
    default void transferTo(Path dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(getInputStream(), Files.newOutputStream(dest));
    }

}
View Code

相关文章: