001/* 002 * Copyright 2012-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 * http://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.boot.web.servlet; 018 019import javax.servlet.MultipartConfigElement; 020 021import org.springframework.util.unit.DataSize; 022 023/** 024 * Factory that can be used to create a {@link MultipartConfigElement}. Size values can be 025 * set using traditional {@literal long} values which are set in bytes or using more 026 * convenient {@link DataSize} variants. 027 * 028 * @author Phillip Webb 029 * @since 1.4.0 030 */ 031public class MultipartConfigFactory { 032 033 private String location; 034 035 private DataSize maxFileSize; 036 037 private DataSize maxRequestSize; 038 039 private DataSize fileSizeThreshold; 040 041 /** 042 * Sets the directory location where files will be stored. 043 * @param location the location 044 */ 045 public void setLocation(String location) { 046 this.location = location; 047 } 048 049 /** 050 * Sets the maximum {@link DataSize size} allowed for uploaded files. 051 * @param maxFileSize the maximum file size 052 */ 053 public void setMaxFileSize(DataSize maxFileSize) { 054 this.maxFileSize = maxFileSize; 055 } 056 057 /** 058 * Sets the maximum size in bytes allowed for uploaded files. 059 * @param maxFileSize the maximum file size 060 * @deprecated since 2.1.0 in favor of {@link #setMaxFileSize(DataSize)} 061 */ 062 @Deprecated 063 public void setMaxFileSize(long maxFileSize) { 064 setMaxFileSize(DataSize.ofBytes(maxFileSize)); 065 } 066 067 /** 068 * Sets the maximum size allowed for uploaded files. Values can use the suffixed "MB" 069 * or "KB" to indicate a Megabyte or Kilobyte size. 070 * @param maxFileSize the maximum file size 071 * @deprecated since 2.1.0 in favor of {@link #setMaxFileSize(DataSize)} 072 */ 073 @Deprecated 074 public void setMaxFileSize(String maxFileSize) { 075 setMaxFileSize(DataSize.parse(maxFileSize)); 076 } 077 078 /** 079 * Sets the maximum {@link DataSize} allowed for multipart/form-data requests. 080 * @param maxRequestSize the maximum request size 081 */ 082 public void setMaxRequestSize(DataSize maxRequestSize) { 083 this.maxRequestSize = maxRequestSize; 084 } 085 086 /** 087 * Sets the maximum size allowed in bytes for multipart/form-data requests. 088 * @param maxRequestSize the maximum request size 089 * @deprecated since 2.1.0 in favor of {@link #setMaxRequestSize(DataSize)} 090 */ 091 @Deprecated 092 public void setMaxRequestSize(long maxRequestSize) { 093 setMaxRequestSize(DataSize.ofBytes(maxRequestSize)); 094 } 095 096 /** 097 * Sets the maximum size allowed for multipart/form-data requests. Values can use the 098 * suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size. 099 * @param maxRequestSize the maximum request size 100 * @deprecated since 2.1.0 in favor of {@link #setMaxRequestSize(DataSize)} 101 */ 102 @Deprecated 103 public void setMaxRequestSize(String maxRequestSize) { 104 setMaxRequestSize(DataSize.parse(maxRequestSize)); 105 } 106 107 /** 108 * Sets the {@link DataSize size} threshold after which files will be written to disk. 109 * @param fileSizeThreshold the file size threshold 110 */ 111 public void setFileSizeThreshold(DataSize fileSizeThreshold) { 112 this.fileSizeThreshold = fileSizeThreshold; 113 } 114 115 /** 116 * Sets the size threshold in bytes after which files will be written to disk. 117 * @param fileSizeThreshold the file size threshold 118 * @deprecated since 2.1.0 in favor of {@link #setFileSizeThreshold(DataSize)} 119 */ 120 @Deprecated 121 public void setFileSizeThreshold(int fileSizeThreshold) { 122 setFileSizeThreshold(DataSize.ofBytes(fileSizeThreshold)); 123 } 124 125 /** 126 * Sets the size threshold after which files will be written to disk. Values can use 127 * the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size. 128 * @param fileSizeThreshold the file size threshold 129 * @deprecated since 2.1.0 in favor of {@link #setFileSizeThreshold(DataSize)} 130 */ 131 @Deprecated 132 public void setFileSizeThreshold(String fileSizeThreshold) { 133 setFileSizeThreshold(DataSize.parse(fileSizeThreshold)); 134 } 135 136 /** 137 * Create a new {@link MultipartConfigElement} instance. 138 * @return the multipart config element 139 */ 140 public MultipartConfigElement createMultipartConfig() { 141 long maxFileSizeBytes = convertToBytes(this.maxFileSize, -1); 142 long maxRequestSizeBytes = convertToBytes(this.maxRequestSize, -1); 143 long fileSizeThresholdBytes = convertToBytes(this.fileSizeThreshold, 0); 144 return new MultipartConfigElement(this.location, maxFileSizeBytes, 145 maxRequestSizeBytes, (int) fileSizeThresholdBytes); 146 } 147 148 /** 149 * Return the amount of bytes from the specified {@link DataSize size}. If the size is 150 * {@code null} or negative, returns {@code defaultValue}. 151 * @param size the data size to handle 152 * @param defaultValue the default value if the size is {@code null} or negative 153 * @return the amount of bytes to use 154 */ 155 private long convertToBytes(DataSize size, int defaultValue) { 156 if (size != null && !size.isNegative()) { 157 return size.toBytes(); 158 } 159 return defaultValue; 160 } 161 162}