file upload max size controller, handler (#999)
* file upload max size controller, handler * add controller advice * update controller * add reset button
This commit is contained in:
parent
86b3ccefb4
commit
ff77354a26
|
@ -41,7 +41,11 @@
|
|||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>${fileupload.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<profiles>
|
||||
<!-- Local -->
|
||||
|
@ -93,6 +97,7 @@
|
|||
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
|
||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||
<deploy-path>enter-location-of-server</deploy-path>
|
||||
<fileupload.version>1.3.2</fileupload.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -3,6 +3,8 @@ package com.baeldung.springmvcforms.configuration;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
@ -26,4 +28,10 @@ class ApplicationConfiguration extends WebMvcConfigurerAdapter {
|
|||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MultipartResolver multipartResolver() {
|
||||
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||
multipartResolver.setMaxUploadSize(5242880);
|
||||
return multipartResolver;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.springmvcforms.controller;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
public class FileUploadController implements HandlerExceptionResolver {
|
||||
|
||||
@RequestMapping(value = "/uploadFile", method = RequestMethod.GET)
|
||||
public String getImageView() {
|
||||
return "file";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
|
||||
public ModelAndView uploadFile(MultipartFile file) throws IOException{
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
|
||||
InputStream in = file.getInputStream();
|
||||
File currDir = new File(".");
|
||||
String path = currDir.getAbsolutePath();
|
||||
FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ file.getOriginalFilename());
|
||||
int ch = 0;
|
||||
while ((ch = in.read()) != -1) {
|
||||
f.write(ch);
|
||||
}
|
||||
f.flush();
|
||||
f.close();
|
||||
|
||||
modelAndView.getModel().put("message", "File uploaded successfully!");
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) {
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
if (exc instanceof MaxUploadSizeExceededException) {
|
||||
modelAndView.getModel().put("message", "File size exceeds limit!");
|
||||
}
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.springmvcforms.interceptor;
|
||||
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
@ControllerAdvice
|
||||
public class FileUploadExceptionAdvice {
|
||||
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
public ModelAndView handleMaxSizeException(MaxUploadSizeExceededException exc, HttpServletRequest request, HttpServletResponse response){
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
modelAndView.getModel().put("message", "File too large!");
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Upload file</title>
|
||||
</head>
|
||||
<body>
|
||||
<c:url value="/uploadFile" var="uploadFileUrl" />
|
||||
<form method="post" enctype="multipart/form-data" action="${uploadFileUrl}">
|
||||
<input type="file" name="file"/>
|
||||
<input type="submit" value="Upload file"/>
|
||||
</form>
|
||||
<br />
|
||||
${message }
|
||||
<br /> <br />
|
||||
<form method="GET" action="${uploadFileUrl}" >
|
||||
<input type="submit" value="Reset" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue