Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
a10abffe
Commit
a10abffe
authored
Aug 21, 2013
by
Phillip Webb
Committed by
Phillip Webb
Sep 06, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add MultiPartConfigFactory
Issue: #55528242
parent
71946f9d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
192 additions
and
0 deletions
+192
-0
MultiPartConfigFactory.java
...amework/boot/context/embedded/MultiPartConfigFactory.java
+123
-0
MultiPartConfigFactoryTests.java
...rk/boot/context/embedded/MultiPartConfigFactoryTests.java
+69
-0
No files found.
spring-boot/src/main/java/org/springframework/boot/context/embedded/MultiPartConfigFactory.java
0 → 100644
View file @
a10abffe
/*
* Copyright 2012-2013 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
*
* http://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
.
boot
.
context
.
embedded
;
import
javax.servlet.MultipartConfigElement
;
import
org.springframework.util.Assert
;
/**
* Factory that can be used to create a {@link MultipartConfigElement}. Size values can be
* set using traditional {@literal long} values or using more readable {@literal String}
* variants that accept KB or MB suffixes, for example:
*
* <pre class="code">
* factory.setMaxFileSize("10Mb");
* factory.setMaxRequestSize("100Kb");
* </pre>
*
* @author Phillip Webb
*/
public
class
MultiPartConfigFactory
{
private
String
location
;
private
long
maxFileSize
=
-
1
;
private
long
maxRequestSize
=
-
1
;
private
int
fileSizeThreshold
=
0
;
/**
* Sets the directory location where files will be stored.
*/
public
void
setLocation
(
String
location
)
{
this
.
location
=
location
;
}
/**
* Sets the maximum size allowed for uploaded files.
* @see #setMaxFileSize(String)
*/
public
void
setMaxFileSize
(
long
maxFileSize
)
{
this
.
maxFileSize
=
maxFileSize
;
}
/**
* Sets the maximum size allowed for uploaded files. Values can use the suffixed "MB"
* or "KB" to indicate a Megabyte or Kilobyte size.
* @see #setMaxFileSize(long)
*/
public
void
setMaxFileSize
(
String
maxFileSize
)
{
this
.
maxFileSize
=
parseSize
(
maxFileSize
);
}
/**
* Sets the maximum size allowed for multipart/form-data requests.
* @see #setMaxRequestSize(String)
*/
public
void
setMaxRequestSize
(
long
maxRequestSize
)
{
this
.
maxRequestSize
=
maxRequestSize
;
}
/**
* Sets the maximum size allowed for multipart/form-data requests. Values can use the
* suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
* @see #setMaxRequestSize(long)
*/
public
void
setMaxRequestSize
(
String
maxRequestSize
)
{
this
.
maxRequestSize
=
parseSize
(
maxRequestSize
);
}
/**
* Sets the size threshold after which files will be written to disk.
* @see #setFileSizeThreshold(String)
*/
public
void
setFileSizeThreshold
(
int
fileSizeThreshold
)
{
this
.
fileSizeThreshold
=
fileSizeThreshold
;
}
/**
* Sets the size threshold after which files will be written to disk. Values can use
* the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
* @see #setFileSizeThreshold(int)
*/
public
void
setFileSizeThreshold
(
String
fileSizeThreshold
)
{
this
.
fileSizeThreshold
=
(
int
)
parseSize
(
fileSizeThreshold
);
}
private
long
parseSize
(
String
size
)
{
Assert
.
hasLength
(
size
,
"Size must not be empty"
);
size
=
size
.
toUpperCase
();
if
(
size
.
endsWith
(
"KB"
))
{
return
Long
.
valueOf
(
size
.
substring
(
0
,
size
.
length
()
-
2
))
*
1024
;
}
if
(
size
.
endsWith
(
"MB"
))
{
return
Long
.
valueOf
(
size
.
substring
(
0
,
size
.
length
()
-
2
))
*
1024
*
1024
;
}
return
Long
.
valueOf
(
size
);
}
/**
* Create a new {@link MultipartConfigElement} instance.
*/
public
MultipartConfigElement
createMultipartConfig
()
{
return
new
MultipartConfigElement
(
this
.
location
,
this
.
maxFileSize
,
this
.
maxRequestSize
,
this
.
fileSizeThreshold
);
}
}
spring-boot/src/test/java/org/springframework/boot/context/embedded/MultiPartConfigFactoryTests.java
0 → 100644
View file @
a10abffe
/*
* Copyright 2012-2013 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
*
* http://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
.
boot
.
context
.
embedded
;
import
javax.servlet.MultipartConfigElement
;
import
org.junit.Test
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Tests for {@link MultiPartConfigFactory}.
*
* @author Phillip Webb
*/
public
class
MultiPartConfigFactoryTests
{
@Test
public
void
sensibleDefaults
()
{
MultiPartConfigFactory
factory
=
new
MultiPartConfigFactory
();
MultipartConfigElement
config
=
factory
.
createMultipartConfig
();
assertThat
(
config
.
getLocation
(),
equalTo
(
""
));
assertThat
(
config
.
getMaxFileSize
(),
equalTo
(-
1L
));
assertThat
(
config
.
getMaxRequestSize
(),
equalTo
(-
1L
));
assertThat
(
config
.
getFileSizeThreshold
(),
equalTo
(
0
));
}
@Test
public
void
create
()
throws
Exception
{
MultiPartConfigFactory
factory
=
new
MultiPartConfigFactory
();
factory
.
setLocation
(
"loc"
);
factory
.
setMaxFileSize
(
1
);
factory
.
setMaxRequestSize
(
2
);
factory
.
setFileSizeThreshold
(
3
);
MultipartConfigElement
config
=
factory
.
createMultipartConfig
();
assertThat
(
config
.
getLocation
(),
equalTo
(
"loc"
));
assertThat
(
config
.
getMaxFileSize
(),
equalTo
(
1L
));
assertThat
(
config
.
getMaxRequestSize
(),
equalTo
(
2L
));
assertThat
(
config
.
getFileSizeThreshold
(),
equalTo
(
3
));
}
@Test
public
void
createWithStringSizes
()
throws
Exception
{
MultiPartConfigFactory
factory
=
new
MultiPartConfigFactory
();
factory
.
setMaxFileSize
(
"1"
);
factory
.
setMaxRequestSize
(
"2kB"
);
factory
.
setFileSizeThreshold
(
"3Mb"
);
MultipartConfigElement
config
=
factory
.
createMultipartConfig
();
assertThat
(
config
.
getMaxFileSize
(),
equalTo
(
1L
));
assertThat
(
config
.
getMaxRequestSize
(),
equalTo
(
2
*
1024L
));
assertThat
(
config
.
getFileSizeThreshold
(),
equalTo
(
3
*
1024
*
1024
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment