Uploading A Files with Spring MVC
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
public class WebbAppInit implements WebApplicationInitializer {
|
||||
|
||||
private int MAX_UPLOAD_SIZE = 2 * 1024 * 1024;
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
|
||||
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
rootContext.register(SpringMvcFileUploadApplication.class);
|
||||
|
||||
// Manage the lifecycle of the root application context
|
||||
servletContext.addListener(new ContextLoaderListener(rootContext));
|
||||
|
||||
// Register and map the dispatcher servlet
|
||||
ServletRegistration.Dynamic dispatcher =
|
||||
servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
|
||||
dispatcher.setLoadOnStartup(1);
|
||||
dispatcher.addMapping("/");
|
||||
MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/temp",
|
||||
MAX_UPLOAD_SIZE, MAX_UPLOAD_SIZE * 2, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.javadevjournal.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
@Controller
|
||||
public class FIleUploadController {
|
||||
|
||||
@GetMapping(value = "/saveFile")
|
||||
public String getFileU(Model model){
|
||||
return "uploadForm";
|
||||
}
|
||||
|
||||
@PostMapping(value = "/saveFile")
|
||||
public String handleFileUpload(@RequestParam("file") MultipartFile file, Model model) {
|
||||
|
||||
//Logic to store data in temp file or in DB
|
||||
model.addAttribute("uploadedFile", file);
|
||||
return "uploadForm";
|
||||
}
|
||||
|
||||
@PostMapping(value = "/saveFiles")
|
||||
public String handleFileUpload(@RequestParam("files") MultipartFile[] files, Model model) {
|
||||
|
||||
//Logic to store data in temp file or in DB
|
||||
model.addAttribute("uploadedFile", files);
|
||||
return "uploadForm";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<div th:if="${uploadedFile}">
|
||||
<h2> Uploaded File Details </h2>
|
||||
<table>
|
||||
<tr>
|
||||
${uploadedFile}
|
||||
<td>FileName:</td>
|
||||
<td>${uploadedFile.fileName}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Type:</td>
|
||||
<td>${uploadedFile.contentType}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
@@ -0,0 +1,24 @@
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<body>
|
||||
|
||||
<div th:if="${uploadedFile}">
|
||||
<h2> Uploaded File Details </h2>
|
||||
<table>
|
||||
<tr th:each="file: ${uploadedFile}">
|
||||
<td th:text="${file.originalFilename}">File Name</td>
|
||||
<td th:text="${file.contentType}">File Type:</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div>
|
||||
<h2> Uploaded New File </h2>
|
||||
<form method="POST" enctype="multipart/form-data" action="/saveFiles">
|
||||
<table>
|
||||
<tr><td>File to upload 1:</td><td><input type="file" name="files" /></td></tr>
|
||||
<tr><td>File to upload 2:</td><td><input type="file" name="files" /></td></tr>
|
||||
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
|
||||
</table>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user