Should be. It's surprisingly easy to get there. It seems that you can go this way from Spring 4 for the time being.
Spring uses an object called "MultipartFile" when uploading files. Let's turn this setting on in Spring and Servlet
Let's set web.xml and application-config.xml (xml with Bean settings).
First of all, the dispatcher's multipart file Specify the upload size. The last item is the size of the temporary files that the server saves. It seems better to specify something explicitly.
web.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!--File upload mode-->
<multipart-config>
<max-file-size>5242880</max-file-size>
<max-request-size>5242880</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
And now Spring can handle multipart files. Let's add Multipart Resolver to the bean.
application-config.xml
<!--File upload resolver-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
It's easy here.
Let's create an upload page! When uploading a file, set ** enctype = "multipart / form-data ** in form.
My source is
The rest is not that special source. (Of course jsp)
ImageUpload.html
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<head>
<meta content="ja" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Please enter your favorite sea or water image</title>
</head>
<body>
<form:form action="imageComplete" method="post"
modelAttribute="ImageUploadForm" enctype="multipart/form-data">
Enter your favorite sea or water image! > <
<input name="image" type="file" />
<input name="submit" type="submit">
</form:form>
</body>
</html>
This is the upload form received by the server side
ImageUploadForm.java
import lombok.Data;
/**
*Registration form
* @author nozawa
*
*/
@Data
public class ImageUploadForm {
@NotNull
private MultipartFile image;
}
Now let's create a server side that receives the uploaded images.
First, enable the upload form.
ImageIOController.java
@Controller
public class ImageIOController {
@ModelAttribute(name = "ImageUploadForm")
public ImageUploadForm initForm(){
ImageUploadForm imageUploadForm = new ImageUploadForm();
/*
Initial setting
*/
return imageUploadForm;
}
}
Next, create a method to display the upload screen.
ImageIOController.java
@RequestMapping(value = "/imageUpload", method = RequestMethod.GET)
public ModelAndView imageUpload() {
ModelAndView mv = new ModelAndView("ImageUpload");
return mv;
}
This is the main. Make a part to receive the image. All you have to do is specify the upload form as an argument! It's super easy.
ImageIOController.java
@RequestMapping(value = "/imageComplete", method = RequestMethod.POST)
public ModelAndView imageUploadComplete(ImageUploadForm imageUploadForm) {
System.out.println(imageUploadForm.getImage().getOriginalFilename());
Integer FileSize = (int) (imageUploadForm.getImage().getSize());
byte[] ImageBinary = new byte[FileSize];
try {
ImageBinary = imageUploadForm.getImage().getBytes();
} catch (IOException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
ModelAndView mv = new ModelAndView("ImageComplete");
return mv;
}
The MultipartFile object in the upload form is They are accessing temporary files that exist on the server. You can also get binary data properly ヾ (.> ﹏ <.) ノ
The whole controller looks like this
ImageIOController.java
package com.TsugaruInfo.controller;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.TsugaruInfo.formmodel.ImageUploadForm;
import com.TsugaruInfo.formmodel.RegisterForm;
@Controller
public class ImageIOController {
@ModelAttribute(name = "ImageUploadForm")
public ImageUploadForm initForm(){
ImageUploadForm imageUploadForm = new ImageUploadForm();
/*
Initial setting
*/
return imageUploadForm;
}
@RequestMapping(value = "/imageUpload", method = RequestMethod.GET)
public ModelAndView imageUpload() {
ModelAndView mv = new ModelAndView("ImageUpload");
return mv;
}
@RequestMapping(value = "/imageComplete", method = RequestMethod.POST)
public ModelAndView imageUploadComplete(ImageUploadForm imageUploadForm) {
System.out.println(imageUploadForm.getImage().getOriginalFilename());
Integer FileSize = (int) (imageUploadForm.getImage().getSize());
byte[] ImageBinary = new byte[FileSize];
try {
ImageBinary = imageUploadForm.getImage().getBytes();
} catch (IOException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
ModelAndView mv = new ModelAndView("ImageComplete");
return mv;
}
}
Let's run it
First open the upload screen
Select an image and send it!
Let's take a look at the console.
I think the server is reading the file name properly.:. ゜ ヽ (´∀`.) ノ ゜.:. ゜
reference 5.17. File upload https://terasolunaorg.github.io/guideline/public_review/ArchitectureInDetail/FileUpload.html
Recommended Posts