Skip to content

Commit ff77354

Browse files
lor6KevinGilmore
authored andcommitted
file upload max size controller, handler (eugenp#999)
* file upload max size controller, handler * add controller advice * update controller * add reset button
1 parent 86b3cce commit ff77354

5 files changed

Lines changed: 108 additions & 1 deletion

File tree

‎spring-mvc-forms/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141
<version>${hibernate-validator.version}</version>
4242
</dependency>
4343

44-
44+
<dependency>
45+
<groupId>commons-fileupload</groupId>
46+
<artifactId>commons-fileupload</artifactId>
47+
<version>${fileupload.version}</version>
48+
</dependency>
4549
</dependencies>
4650
<profiles>
4751
<!-- Local -->
@@ -93,6 +97,7 @@
9397
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
9498
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
9599
<deploy-path>enter-location-of-server</deploy-path>
100+
<fileupload.version>1.3.2</fileupload.version>
96101
</properties>
97102

98103
</project>

‎spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.ComponentScan;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.multipart.MultipartResolver;
7+
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
68
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
79
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
810
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@@ -26,4 +28,10 @@ public InternalResourceViewResolver jspViewResolver() {
2628
return bean;
2729
}
2830

31+
@Bean
32+
public MultipartResolver multipartResolver() {
33+
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
34+
multipartResolver.setMaxUploadSize(5242880);
35+
return multipartResolver;
36+
}
2937
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.springmvcforms.controller;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.multipart.MaxUploadSizeExceededException;
13+
import org.springframework.web.multipart.MultipartFile;
14+
import org.springframework.web.servlet.HandlerExceptionResolver;
15+
import org.springframework.web.servlet.ModelAndView;
16+
17+
@Controller
18+
public class FileUploadController implements HandlerExceptionResolver {
19+
20+
@RequestMapping(value = "/uploadFile", method = RequestMethod.GET)
21+
public String getImageView() {
22+
return "file";
23+
}
24+
25+
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
26+
public ModelAndView uploadFile(MultipartFile file) throws IOException{
27+
ModelAndView modelAndView = new ModelAndView("file");
28+
29+
InputStream in = file.getInputStream();
30+
File currDir = new File(".");
31+
String path = currDir.getAbsolutePath();
32+
FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ file.getOriginalFilename());
33+
int ch = 0;
34+
while ((ch = in.read()) != -1) {
35+
f.write(ch);
36+
}
37+
f.flush();
38+
f.close();
39+
40+
modelAndView.getModel().put("message", "File uploaded successfully!");
41+
return modelAndView;
42+
}
43+
44+
@Override
45+
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) {
46+
ModelAndView modelAndView = new ModelAndView("file");
47+
if (exc instanceof MaxUploadSizeExceededException) {
48+
modelAndView.getModel().put("message", "File size exceeds limit!");
49+
}
50+
return modelAndView;
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.springmvcforms.interceptor;
2+
3+
import org.springframework.web.servlet.ModelAndView;
4+
import javax.servlet.http.HttpServletRequest;
5+
import javax.servlet.http.HttpServletResponse;
6+
import org.springframework.web.multipart.MaxUploadSizeExceededException;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
10+
@ControllerAdvice
11+
public class FileUploadExceptionAdvice {
12+
13+
@ExceptionHandler(MaxUploadSizeExceededException.class)
14+
public ModelAndView handleMaxSizeException(MaxUploadSizeExceededException exc, HttpServletRequest request, HttpServletResponse response){
15+
ModelAndView modelAndView = new ModelAndView("file");
16+
modelAndView.getModel().put("message", "File too large!");
17+
return modelAndView;
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2+
pageEncoding="ISO-8859-1"%>
3+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
4+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5+
<html>
6+
<head>
7+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
8+
<title>Upload file</title>
9+
</head>
10+
<body>
11+
<c:url value="/uploadFile" var="uploadFileUrl" />
12+
<form method="post" enctype="multipart/form-data" action="${uploadFileUrl}">
13+
<input type="file" name="file"/>
14+
<input type="submit" value="Upload file"/>
15+
</form>
16+
<br />
17+
${message }
18+
<br /> <br />
19+
<form method="GET" action="${uploadFileUrl}" >
20+
<input type="submit" value="Reset" />
21+
</form>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)