Add file upload example
This commit is contained in:
parent
09a1ebfa07
commit
b7d0b2947a
@ -36,8 +36,6 @@ public class ClientWebConfig extends WebMvcConfigurerAdapter {
|
|||||||
super.addViewControllers(registry);
|
super.addViewControllers(registry);
|
||||||
|
|
||||||
registry.addViewController("/sample.html");
|
registry.addViewController("/sample.html");
|
||||||
registry.addViewController("/fileUpload.html");
|
|
||||||
registry.addViewController("/fileUploadForm.html");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -14,8 +14,7 @@ import org.springframework.web.servlet.DispatcherServlet;
|
|||||||
|
|
||||||
public class MainWebAppInitializer implements WebApplicationInitializer {
|
public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||||
|
|
||||||
private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5
|
private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp";
|
||||||
// MB
|
|
||||||
private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB
|
private static final int MAX_UPLOAD_SIZE = 5 * 1024 * 1024; // 5 MB
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,6 +4,7 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
@ -27,15 +28,14 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|||||||
// return new StandardServletMultipartResolver();
|
// return new StandardServletMultipartResolver();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// @Bean(name = "multipartResolver")
|
@Bean(name = "multipartResolver")
|
||||||
// public CommonsMultipartResolver multipartResolver() {
|
public CommonsMultipartResolver multipartResolver() {
|
||||||
//
|
|
||||||
// final CommonsMultipartResolver multipartResolver = new
|
final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||||
// CommonsMultipartResolver();
|
multipartResolver.setMaxUploadSize(100000);
|
||||||
// multipartResolver.setMaxUploadSize(100000);
|
|
||||||
//
|
return multipartResolver;
|
||||||
// return multipartResolver;
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||||
|
@ -1,18 +1,5 @@
|
|||||||
package org.baeldung.web.controller;
|
package org.baeldung.web.controller;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
import org.apache.commons.fileupload.FileItem;
|
|
||||||
import org.apache.commons.fileupload.FileUploadException;
|
|
||||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
||||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
||||||
import org.apache.commons.io.FileUtils;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.ModelMap;
|
import org.springframework.ui.ModelMap;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -23,58 +10,23 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
@Controller
|
@Controller
|
||||||
public class FileUploadController {
|
public class FileUploadController {
|
||||||
|
|
||||||
@RequestMapping(value = "/addFile1", method = RequestMethod.POST)
|
@RequestMapping(value = "/fileUpload", method = RequestMethod.GET)
|
||||||
|
public String displayForm() {
|
||||||
|
|
||||||
|
return "fileUploadForm";
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
|
||||||
public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) {
|
public String submit(@RequestParam("file") final MultipartFile file, final ModelMap modelMap) {
|
||||||
|
|
||||||
modelMap.addAttribute("fileName", file.getOriginalFilename());
|
modelMap.addAttribute("file", file);
|
||||||
modelMap.addAttribute("fileType", file.getContentType());
|
|
||||||
|
|
||||||
return "fileUploadView";
|
return "fileUploadView";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/addFile2", method = RequestMethod.POST)
|
@RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
|
||||||
public String submit(final HttpServletRequest request, final HttpServletResponse response,
|
public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) {
|
||||||
final ModelMap modelMap) {
|
|
||||||
|
|
||||||
final String TEMP_PATH = "/tmp/";
|
|
||||||
|
|
||||||
try {
|
|
||||||
|
|
||||||
final DiskFileItemFactory factory = new DiskFileItemFactory();
|
|
||||||
|
|
||||||
// Configure a repository (to ensure a secure temp location is used)
|
|
||||||
final File repository = new File(TEMP_PATH);
|
|
||||||
factory.setRepository(repository);
|
|
||||||
|
|
||||||
// Create a new file upload handler
|
|
||||||
final ServletFileUpload upload = new ServletFileUpload(factory);
|
|
||||||
|
|
||||||
// Parse the request
|
|
||||||
final List<FileItem> items = upload.parseRequest(request);
|
|
||||||
|
|
||||||
final Iterator<FileItem> iter = items.iterator();
|
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
|
||||||
|
|
||||||
final FileItem item = iter.next();
|
|
||||||
|
|
||||||
if (!item.isFormField()) {
|
|
||||||
|
|
||||||
final File targetFile = new File(TEMP_PATH + item.getName());
|
|
||||||
FileUtils.copyInputStreamToFile(item.getInputStream(), targetFile);
|
|
||||||
|
|
||||||
modelMap.addAttribute("fileName", item.getName());
|
|
||||||
modelMap.addAttribute("fileType", item.getContentType());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (final FileUploadException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
} catch (final IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
modelMap.addAttribute("files", files);
|
||||||
return "fileUploadView";
|
return "fileUploadView";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,13 @@
|
|||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<h3>Enter The File to Upload (MultipartFile handling)</h3>
|
<h3>Enter The File to Upload</h3>
|
||||||
|
|
||||||
<form:form method="POST" action="/spring-mvc-java/addFile1" enctype="multipart/form-data">
|
<form:form method="POST" action="/spring-mvc-java/uploadFile" enctype="multipart/form-data">
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Select a file to upload</td>
|
<td>Select a file to upload (Single file)</td>
|
||||||
<td><input type="file" name="file" /></td>
|
<td><input type="file" name="file" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@ -26,14 +26,22 @@
|
|||||||
|
|
||||||
<br />
|
<br />
|
||||||
|
|
||||||
<h3>Enter The File to Upload (HttpServletRequest handling)</h3>
|
<h3>Enter The Files to Upload (Multiple files)</h3>
|
||||||
|
|
||||||
<form:form method="POST" action="/spring-mvc-java/addFile2" enctype="multipart/form-data">
|
<form:form method="POST" action="/spring-mvc-java/uploadMultiFile" enctype="multipart/form-data">
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Select a file to upload</td>
|
<td>Select a file to upload</td>
|
||||||
<td><input type="file" name="file" /></td>
|
<td><input type="file" name="files" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Select a file to upload</td>
|
||||||
|
<td><input type="file" name="files" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Select a file to upload</td>
|
||||||
|
<td><input type="file" name="files" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><input type="submit" value="Submit" /></td>
|
<td><input type="submit" value="Submit" /></td>
|
||||||
|
@ -5,16 +5,32 @@
|
|||||||
<title>Spring MVC File Upload</title>
|
<title>Spring MVC File Upload</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2>Submitted File</h2>
|
|
||||||
|
<h2>Submitted File (Single)</h2>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td>OriginalFileName :</td>
|
<td>OriginalFileName :</td>
|
||||||
<td>${fileName}</td>
|
<td>${file.originalFilename}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Type :</td>
|
<td>Type :</td>
|
||||||
<td>${fileType}</td>
|
<td>${file.contentType}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
<br />
|
||||||
|
|
||||||
|
<h2>Submitted Files (Multiple)</h2>
|
||||||
|
<table>
|
||||||
|
<c:forEach items="${files}" var="file">
|
||||||
|
<tr>
|
||||||
|
<td>OriginalFileName :</td>
|
||||||
|
<td>${file.originalFilename}</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Type :</td>
|
||||||
|
<td>${file.contentType}</td>
|
||||||
|
</tr>
|
||||||
|
</c:forEach>
|
||||||
|
</table>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
x
Reference in New Issue
Block a user