001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.multipart;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022
023import org.springframework.core.io.InputStreamSource;
024
025/**
026 * A representation of an uploaded file received in a multipart request.
027 *
028 * <p>The file contents are either stored in memory or temporarily on disk.
029 * In either case, the user is responsible for copying file contents to a
030 * session-level or persistent store as and if desired. The temporary storage
031 * will be cleared at the end of request processing.
032 *
033 * @author Juergen Hoeller
034 * @author Trevor D. Cook
035 * @since 29.09.2003
036 * @see org.springframework.web.multipart.MultipartHttpServletRequest
037 * @see org.springframework.web.multipart.MultipartResolver
038 */
039public interface MultipartFile extends InputStreamSource {
040
041        /**
042         * Return the name of the parameter in the multipart form.
043         * @return the name of the parameter (never {@code null} or empty)
044         */
045        String getName();
046
047        /**
048         * Return the original filename in the client's filesystem.
049         * <p>This may contain path information depending on the browser used,
050         * but it typically will not with any other than Opera.
051         * @return the original filename, or the empty String if no file has been chosen
052         * in the multipart form, or {@code null} if not defined or not available
053         * @see org.apache.commons.fileupload.FileItem#getName()
054         * @see org.springframework.web.multipart.commons.CommonsMultipartFile#setPreserveFilename
055         */
056        String getOriginalFilename();
057
058        /**
059         * Return the content type of the file.
060         * @return the content type, or {@code null} if not defined
061         * (or no file has been chosen in the multipart form)
062         */
063        String getContentType();
064
065        /**
066         * Return whether the uploaded file is empty, that is, either no file has
067         * been chosen in the multipart form or the chosen file has no content.
068         */
069        boolean isEmpty();
070
071        /**
072         * Return the size of the file in bytes.
073         * @return the size of the file, or 0 if empty
074         */
075        long getSize();
076
077        /**
078         * Return the contents of the file as an array of bytes.
079         * @return the contents of the file as bytes, or an empty byte array if empty
080         * @throws IOException in case of access errors (if the temporary store fails)
081         */
082        byte[] getBytes() throws IOException;
083
084        /**
085         * Return an InputStream to read the contents of the file from.
086         * <p>The user is responsible for closing the returned stream.
087         * @return the contents of the file as stream, or an empty stream if empty
088         * @throws IOException in case of access errors (if the temporary store fails)
089         */
090        @Override
091        InputStream getInputStream() throws IOException;
092
093        /**
094         * Transfer the received file to the given destination file.
095         * <p>This may either move the file in the filesystem, copy the file in the
096         * filesystem, or save memory-held contents to the destination file. If the
097         * destination file already exists, it will be deleted first.
098         * <p>If the target file has been moved in the filesystem, this operation
099         * cannot be invoked again afterwards. Therefore, call this method just once
100         * in order to work with any storage mechanism.
101         * <p><b>NOTE:</b> Depending on the underlying provider, temporary storage
102         * may be container-dependent, including the base directory for relative
103         * destinations specified here (e.g. with Servlet 3.0 multipart handling).
104         * For absolute destinations, the target file may get renamed/moved from its
105         * temporary location or newly copied, even if a temporary copy already exists.
106         * @param dest the destination file (typically absolute)
107         * @throws IOException in case of reading or writing errors
108         * @throws IllegalStateException if the file has already been moved
109         * in the filesystem and is not available anymore for another transfer
110         * @see org.apache.commons.fileupload.FileItem#write(File)
111         * @see javax.servlet.http.Part#write(String)
112         */
113        void transferTo(File dest) throws IOException, IllegalStateException;
114
115}