001/*
002 * Copyright 2002-2018 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;
022import java.nio.file.Files;
023import java.nio.file.Path;
024
025import org.springframework.core.io.InputStreamSource;
026import org.springframework.core.io.Resource;
027import org.springframework.lang.Nullable;
028import org.springframework.util.FileCopyUtils;
029
030/**
031 * A representation of an uploaded file received in a multipart request.
032 *
033 * <p>The file contents are either stored in memory or temporarily on disk.
034 * In either case, the user is responsible for copying file contents to a
035 * session-level or persistent store as and if desired. The temporary storage
036 * will be cleared at the end of request processing.
037 *
038 * @author Juergen Hoeller
039 * @author Trevor D. Cook
040 * @since 29.09.2003
041 * @see org.springframework.web.multipart.MultipartHttpServletRequest
042 * @see org.springframework.web.multipart.MultipartResolver
043 */
044public interface MultipartFile extends InputStreamSource {
045
046        /**
047         * Return the name of the parameter in the multipart form.
048         * @return the name of the parameter (never {@code null} or empty)
049         */
050        String getName();
051
052        /**
053         * Return the original filename in the client's filesystem.
054         * <p>This may contain path information depending on the browser used,
055         * but it typically will not with any other than Opera.
056         * @return the original filename, or the empty String if no file has been chosen
057         * in the multipart form, or {@code null} if not defined or not available
058         * @see org.apache.commons.fileupload.FileItem#getName()
059         * @see org.springframework.web.multipart.commons.CommonsMultipartFile#setPreserveFilename
060         */
061        @Nullable
062        String getOriginalFilename();
063
064        /**
065         * Return the content type of the file.
066         * @return the content type, or {@code null} if not defined
067         * (or no file has been chosen in the multipart form)
068         */
069        @Nullable
070        String getContentType();
071
072        /**
073         * Return whether the uploaded file is empty, that is, either no file has
074         * been chosen in the multipart form or the chosen file has no content.
075         */
076        boolean isEmpty();
077
078        /**
079         * Return the size of the file in bytes.
080         * @return the size of the file, or 0 if empty
081         */
082        long getSize();
083
084        /**
085         * Return the contents of the file as an array of bytes.
086         * @return the contents of the file as bytes, or an empty byte array if empty
087         * @throws IOException in case of access errors (if the temporary store fails)
088         */
089        byte[] getBytes() throws IOException;
090
091        /**
092         * Return an InputStream to read the contents of the file from.
093         * <p>The user is responsible for closing the returned stream.
094         * @return the contents of the file as stream, or an empty stream if empty
095         * @throws IOException in case of access errors (if the temporary store fails)
096         */
097        @Override
098        InputStream getInputStream() throws IOException;
099
100        /**
101         * Return a Resource representation of this MultipartFile. This can be used
102         * as input to the {@code RestTemplate} or the {@code WebClient} to expose
103         * content length and the filename along with the InputStream.
104         * @return this MultipartFile adapted to the Resource contract
105         * @since 5.1
106         */
107        default Resource getResource() {
108                return new MultipartFileResource(this);
109        }
110
111        /**
112         * Transfer the received file to the given destination file.
113         * <p>This may either move the file in the filesystem, copy the file in the
114         * filesystem, or save memory-held contents to the destination file. If the
115         * destination file already exists, it will be deleted first.
116         * <p>If the target file has been moved in the filesystem, this operation
117         * cannot be invoked again afterwards. Therefore, call this method just once
118         * in order to work with any storage mechanism.
119         * <p><b>NOTE:</b> Depending on the underlying provider, temporary storage
120         * may be container-dependent, including the base directory for relative
121         * destinations specified here (e.g. with Servlet 3.0 multipart handling).
122         * For absolute destinations, the target file may get renamed/moved from its
123         * temporary location or newly copied, even if a temporary copy already exists.
124         * @param dest the destination file (typically absolute)
125         * @throws IOException in case of reading or writing errors
126         * @throws IllegalStateException if the file has already been moved
127         * in the filesystem and is not available anymore for another transfer
128         * @see org.apache.commons.fileupload.FileItem#write(File)
129         * @see javax.servlet.http.Part#write(String)
130         */
131        void transferTo(File dest) throws IOException, IllegalStateException;
132
133        /**
134         * Transfer the received file to the given destination file.
135         * <p>The default implementation simply copies the file input stream.
136         * @since 5.1
137         * @see #getInputStream()
138         * @see #transferTo(File)
139         */
140        default void transferTo(Path dest) throws IOException, IllegalStateException {
141                FileCopyUtils.copy(getInputStream(), Files.newOutputStream(dest));
142        }
143
144}