Permalink
Browse files

UploadedFile - Return the size of the uploaded file

  • Loading branch information...
1 parent b5ad875 commit 9cbcfae76bc68457394a00046919efbb68e158c1 Tiago de Freitas Lima committed Nov 7, 2012
View
@@ -3,6 +3,7 @@ out
.project
.settings
.scala_dependencies
+.metadata
!vraptor-blank-project/.classpath
!vraptor-blank-project/.settings/
!vraptor-blank-project/.project
@@ -154,7 +154,7 @@ protected void reportSizeLimitExceeded(final SizeLimitExceededException e) {
protected void processFile(FileItem item, String name) {
try {
- UploadedFile upload = new DefaultUploadedFile(item.getInputStream(), item.getName(), item.getContentType());
+ UploadedFile upload = new DefaultUploadedFile(item.getInputStream(), item.getName(), item.getContentType(), item.getSize());
parameters.setParameter(name, name);
request.setAttribute(name, upload);
@@ -34,12 +34,15 @@
private final InputStream content;
+ private final long size;
+
public DefaultUploadedFile(InputStream content, String completeFileName,
- String contentType) {
+ String contentType, long size) {
this.content = content;
this.fileName = REGEX_REMOVE_SLASHES.matcher(completeFileName).replaceAll("$1");
this.completeFileName = completeFileName;
this.contentType = contentType;
+ this.size = size;
}
@Override
@@ -64,4 +67,8 @@ public String getFileName() {
public String getCompleteFileName() {
return this.completeFileName;
}
+
+ public long getSize() {
+ return size;
+ }
}
@@ -127,7 +127,7 @@ public void intercept(InterceptorStack stack, ResourceMethod method, Object reso
logger.debug("{} is a file", name);
String fileName = getFileName(part);
- UploadedFile upload = new DefaultUploadedFile(part.getInputStream(), fileName, part.getContentType());
+ UploadedFile upload = new DefaultUploadedFile(part.getInputStream(), fileName, part.getContentType(), part.getSize());
parameters.setParameter(name, name);
request.setAttribute(name, upload);
@@ -42,5 +42,11 @@
* client
*/
String getFileName();
+
+ /**
+ *
+ * @return Returns the file of uploaded file.
+ */
+ long getSize();
}
@@ -13,7 +13,7 @@
@Test
public void usingUnixLikeSeparators() throws Exception {
- DefaultUploadedFile file = new DefaultUploadedFile(CONTENT, "/a/unix/path/file.txt", "text/plain");
+ DefaultUploadedFile file = new DefaultUploadedFile(CONTENT, "/a/unix/path/file.txt", "text/plain", 0);
assertThat(file.getFileName(), is("file.txt"));
assertThat(file.getCompleteFileName(), is("/a/unix/path/file.txt"));
@@ -22,7 +22,7 @@ public void usingUnixLikeSeparators() throws Exception {
@Test
public void usingWindowsLikeSeparators() throws Exception {
- DefaultUploadedFile file = new DefaultUploadedFile(CONTENT, "C:\\a\\windows\\path\\file.txt", "text/plain");
+ DefaultUploadedFile file = new DefaultUploadedFile(CONTENT, "C:\\a\\windows\\path\\file.txt", "text/plain", 0);
assertThat(file.getFileName(), is("file.txt"));
assertThat(file.getCompleteFileName(), is("C:\\a\\windows\\path\\file.txt"));
@@ -31,7 +31,7 @@ public void usingWindowsLikeSeparators() throws Exception {
@Test
public void usingOnlyFilename() {
- DefaultUploadedFile file = new DefaultUploadedFile(CONTENT, "file.txt", "text/plain");
+ DefaultUploadedFile file = new DefaultUploadedFile(CONTENT, "file.txt", "text/plain", 0);
assertThat(file.getFileName(), is("file.txt"));
assertThat(file.getCompleteFileName(), is("file.txt"));

0 comments on commit 9cbcfae

Please sign in to comment.