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);
|
||||
|
||||
registry.addViewController("/sample.html");
|
||||
registry.addViewController("/fileUpload.html");
|
||||
registry.addViewController("/fileUploadForm.html");
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -14,8 +14,7 @@ import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class MainWebAppInitializer implements WebApplicationInitializer {
|
||||
|
||||
private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp"; // 5
|
||||
// MB
|
||||
private static final String TMP_FOLDER = "C:/Users/ivan/Desktop/tmp";
|
||||
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.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
@ -27,15 +28,14 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
// return new StandardServletMultipartResolver();
|
||||
// }
|
||||
|
||||
// @Bean(name = "multipartResolver")
|
||||
// public CommonsMultipartResolver multipartResolver() {
|
||||
//
|
||||
// final CommonsMultipartResolver multipartResolver = new
|
||||
// CommonsMultipartResolver();
|
||||
// multipartResolver.setMaxUploadSize(100000);
|
||||
//
|
||||
// return multipartResolver;
|
||||
// }
|
||||
@Bean(name = "multipartResolver")
|
||||
public CommonsMultipartResolver multipartResolver() {
|
||||
|
||||
final CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||
multipartResolver.setMaxUploadSize(100000);
|
||||
|
||||
return multipartResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
|
@ -1,18 +1,5 @@
|
||||
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.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -23,58 +10,23 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
@Controller
|
||||
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) {
|
||||
|
||||
modelMap.addAttribute("fileName", file.getOriginalFilename());
|
||||
modelMap.addAttribute("fileType", file.getContentType());
|
||||
|
||||
modelMap.addAttribute("file", file);
|
||||
return "fileUploadView";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/addFile2", method = RequestMethod.POST)
|
||||
public String submit(final HttpServletRequest request, final HttpServletResponse response,
|
||||
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();
|
||||
}
|
||||
@RequestMapping(value = "/uploadMultiFile", method = RequestMethod.POST)
|
||||
public String submit(@RequestParam("files") final MultipartFile[] files, final ModelMap modelMap) {
|
||||
|
||||
modelMap.addAttribute("files", files);
|
||||
return "fileUploadView";
|
||||
}
|
||||
}
|
||||
|
@ -8,13 +8,13 @@
|
||||
|
||||
<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>
|
||||
<tr>
|
||||
<td>Select a file to upload</td>
|
||||
<td>Select a file to upload (Single file)</td>
|
||||
<td><input type="file" name="file" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
@ -24,16 +24,24 @@
|
||||
|
||||
</form:form>
|
||||
|
||||
<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>
|
||||
<tr>
|
||||
<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>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
|
@ -5,16 +5,32 @@
|
||||
<title>Spring MVC File Upload</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Submitted File</h2>
|
||||
|
||||
<h2>Submitted File (Single)</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td>OriginalFileName :</td>
|
||||
<td>${fileName}</td>
|
||||
<td>${file.originalFilename}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Type :</td>
|
||||
<td>${fileType}</td>
|
||||
<td>${file.contentType}</td>
|
||||
</tr>
|
||||
</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>
|
||||
</html>
|
Loading…
x
Reference in New Issue
Block a user