spring MultipartUtils 源码

  • 2022-08-08
  • 浏览 (350)

spring MultipartUtils 代码

文件路径:/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartUtils.java

/*
 * Copyright 2002-2022 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.http.codec.multipart;

import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMessage;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;

/**
 * Various static utility methods for dealing with multipart parsing.
 * @author Arjen Poutsma
 * @since 5.3
 */
abstract class MultipartUtils {

	/**
	 * Return the character set of the given headers, as defined in the
	 * {@link HttpHeaders#getContentType()} header.
	 */
	public static Charset charset(HttpHeaders headers) {
		MediaType contentType = headers.getContentType();
		if (contentType != null) {
			Charset charset = contentType.getCharset();
			if (charset != null) {
				return charset;
			}
		}
		return StandardCharsets.UTF_8;
	}

	@Nullable
	public static byte[] boundary(HttpMessage message, Charset headersCharset) {
		MediaType contentType = message.getHeaders().getContentType();
		if (contentType != null) {
			String boundary = contentType.getParameter("boundary");
			if (boundary != null) {
				int len = boundary.length();
				if (len > 2 && boundary.charAt(0) == '"' && boundary.charAt(len - 1) == '"') {
					boundary = boundary.substring(1, len - 1);
				}
				return boundary.getBytes(headersCharset);
			}
		}
		return null;
	}


	/**
	 * Concatenates the given array of byte arrays.
	 */
	public static byte[] concat(byte[]... byteArrays) {
		int len = 0;
		for (byte[] byteArray : byteArrays) {
			len += byteArray.length;
		}
		byte[] result = new byte[len];
		len = 0;
		for (byte[] byteArray : byteArrays) {
			System.arraycopy(byteArray, 0, result, len, byteArray.length);
			len += byteArray.length;
		}
		return result;
	}

	/**
	 * Slices the given buffer to the given index (exclusive).
	 */
	public static DataBuffer sliceTo(DataBuffer buf, int idx) {
		int pos = buf.readPosition();
		int len = idx - pos + 1;
		return buf.retainedSlice(pos, len);
	}

	/**
	 * Slices the given buffer from the given index (inclusive).
	 */
	public static DataBuffer sliceFrom(DataBuffer buf, int idx) {
		int len = buf.writePosition() - idx - 1;
		return buf.retainedSlice(idx + 1, len);
	}

	public static void closeChannel(Channel channel) {
		try {
			if (channel.isOpen()) {
				channel.close();
			}
		}
		catch (IOException ignore) {
		}
	}

	public static void deleteFile(Path file) {
		try {
			Files.delete(file);
		}
		catch (IOException ignore) {
		}
	}

	public static boolean isFormField(HttpHeaders headers) {
		MediaType contentType = headers.getContentType();
		return (contentType == null || MediaType.TEXT_PLAIN.equalsTypeAndSubtype(contentType))
				&& headers.getContentDisposition().getFilename() == null;
	}
}

相关信息

spring 源码目录

相关文章

spring DefaultPartEvents 源码

spring DefaultPartHttpMessageReader 源码

spring DefaultParts 源码

spring FilePart 源码

spring FilePartEvent 源码

spring FileStorage 源码

spring FormFieldPart 源码

spring FormPartEvent 源码

spring MultipartHttpMessageReader 源码

spring MultipartHttpMessageWriter 源码

0  赞