Spring implementation file upload of sample code
- 2020-04-01 02:18:46
- OfStack
In actual development, it is common to encounter the ability to upload files to the server side. Spring inherits the commons-fileupload plug-in for file uploading. Divided into the front-end JSP preparation and background Controller preparation.
The preliminary preparation is to introduce the jar package Commons -fileupload. The configuration in pom.xml is as follows:
<!-- File upload, spring This functionality is integrated -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
Then configure the interceptor and add it in dispatcher-servlet.xml
<!-- Implement file upload so that once a certain Request Is a MultipartRequest , it will be the first to be MultipartResolver Process, and then forward the corresponding Controller -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Set the maximum size of the uploaded file to 1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
Direct code:
1, front-end JSP preparation
The upload. JSP
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="resources/js/jquery.js"></script>
</head>
<body>
<form method="post" action="upload.do" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
</body>
</html>
2, background UploadController write
package nju.software.xkxt.web.controller;
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.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@Controller
public class UploadController {
@RequestMapping(value = "upload_enter.do", method = RequestMethod.GET)
public String enter(HttpServletRequest request,
HttpServletResponse response, ModelMap model) throws IOException {
//Enter the download interface
return "upload";
}
@RequestMapping(value = "upload.do", method = RequestMethod.POST)
public void upload(HttpServletRequest request,
HttpServletResponse response, ModelMap model) throws IOException {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//Get the uploaded file
MultipartFile mFile = multipartRequest.getFile("file");
//Gets the path to the upload server
String path = request.getSession().getServletContext()
.getRealPath("/WEB-INF/upload/");
//Get the uploaded file The name of the file
String filename = mFile.getOriginalFilename();
InputStream inputStream = mFile.getInputStream();
byte[] b = new byte[1048576];
int length = inputStream.read(b);
path += "\" + filename;
//The file stream is written to the server side
FileOutputStream outputStream = new FileOutputStream(path);
outputStream.write(b, 0, length);
inputStream.close();
outputStream.close();
}
}
This makes it easy to upload files.