001/* 002 * Copyright 2012-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 * 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.autoconfigure.web; 018 019import javax.servlet.MultipartConfigElement; 020 021import org.springframework.boot.context.properties.ConfigurationProperties; 022import org.springframework.boot.web.servlet.MultipartConfigFactory; 023import org.springframework.util.StringUtils; 024 025/** 026 * Properties to be used in configuring a {@link MultipartConfigElement}. 027 * <ul> 028 * <li>{@link #getLocation() location} specifies the directory where uploaded files will 029 * be stored. When not specified, a temporary directory will be used.</li> 030 * <li>{@link #getMaxFileSize() max-file-size} specifies the maximum size permitted for 031 * uploaded files. The default is 1MB.</li> 032 * <li>{@link #getMaxRequestSize() max-request-size} specifies the maximum size allowed 033 * for {@literal multipart/form-data} requests. The default is 10MB.</li> 034 * <li>{@link #getFileSizeThreshold() file-size-threshold} specifies the size threshold 035 * after which files will be written to disk. The default is 0.</li> 036 * </ul> 037 * <p> 038 * These properties are ultimately passed to {@link MultipartConfigFactory} which means 039 * you may specify numeric values using {@literal long} values or using more readable 040 * {@literal String} variants that accept {@literal KB} or {@literal MB} suffixes. 041 * 042 * @author Josh Long 043 * @author Toshiaki Maki 044 * @since 1.1.0 045 */ 046@ConfigurationProperties(prefix = "spring.http.multipart", ignoreUnknownFields = false) 047public class MultipartProperties { 048 049 /** 050 * Enable support of multipart uploads. 051 */ 052 private boolean enabled = true; 053 054 /** 055 * Intermediate location of uploaded files. 056 */ 057 private String location; 058 059 /** 060 * Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or 061 * kilobytes respectively. 062 */ 063 private String maxFileSize = "1MB"; 064 065 /** 066 * Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or 067 * kilobytes respectively. 068 */ 069 private String maxRequestSize = "10MB"; 070 071 /** 072 * Threshold after which files will be written to disk. Values can use the suffixes 073 * "MB" or "KB" to indicate megabytes or kilobytes respectively. 074 */ 075 private String fileSizeThreshold = "0"; 076 077 /** 078 * Whether to resolve the multipart request lazily at the time of file or parameter 079 * access. 080 */ 081 private boolean resolveLazily = false; 082 083 public boolean getEnabled() { 084 return this.enabled; 085 } 086 087 public void setEnabled(boolean enabled) { 088 this.enabled = enabled; 089 } 090 091 public String getLocation() { 092 return this.location; 093 } 094 095 public void setLocation(String location) { 096 this.location = location; 097 } 098 099 public String getMaxFileSize() { 100 return this.maxFileSize; 101 } 102 103 public void setMaxFileSize(String maxFileSize) { 104 this.maxFileSize = maxFileSize; 105 } 106 107 public String getMaxRequestSize() { 108 return this.maxRequestSize; 109 } 110 111 public void setMaxRequestSize(String maxRequestSize) { 112 this.maxRequestSize = maxRequestSize; 113 } 114 115 public String getFileSizeThreshold() { 116 return this.fileSizeThreshold; 117 } 118 119 public void setFileSizeThreshold(String fileSizeThreshold) { 120 this.fileSizeThreshold = fileSizeThreshold; 121 } 122 123 public boolean isResolveLazily() { 124 return this.resolveLazily; 125 } 126 127 public void setResolveLazily(boolean resolveLazily) { 128 this.resolveLazily = resolveLazily; 129 } 130 131 /** 132 * Create a new {@link MultipartConfigElement} using the properties. 133 * @return a new {@link MultipartConfigElement} configured using there properties 134 */ 135 public MultipartConfigElement createMultipartConfig() { 136 MultipartConfigFactory factory = new MultipartConfigFactory(); 137 if (StringUtils.hasText(this.fileSizeThreshold)) { 138 factory.setFileSizeThreshold(this.fileSizeThreshold); 139 } 140 if (StringUtils.hasText(this.location)) { 141 factory.setLocation(this.location); 142 } 143 if (StringUtils.hasText(this.maxRequestSize)) { 144 factory.setMaxRequestSize(this.maxRequestSize); 145 } 146 if (StringUtils.hasText(this.maxFileSize)) { 147 factory.setMaxFileSize(this.maxFileSize); 148 } 149 return factory.createMultipartConfig(); 150 } 151 152}