Implementation of file upload and download sample under SpringMVC framework
- 2020-06-07 04:35:01
- OfStack
In javaEE in eclipse: Import the necessary racks
web.xml profile:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- configuration SpringMVC the DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- configuration HiddenHttpMethodFilter: the POST The request to DELETE , PUT request -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
spring bean configuration file springmvc.xml;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Configure packages for automatic scanning -->
<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
<!-- Configure the view parser -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--
default-servlet-handler Will be in SpringMVC Definition in context 1 a DefaultServletHttpRequestHandler,
It's going to go in DispatcherServlet The request for screening , If the request is found to be unmapped , The request is forwarded WEB Application server by default
Servlet To deal with . If it is not a static resource request DispatcherServlet To continue processing
1 a WEB Application server by default Servlet All of the names default.
If used WEB Server default Servlet The name is not default , you need to pass default-servlet-name Property is specified explicitly
-->
<mvc:default-servlet-handler/>
<!-- 1 I'm going to configure this <mvc:annotation-driven ></mvc:annotation-driven> .
Due to... requestmapping The request will not be implemented. Using this, it will make requestmapping request 1 Set the implementation
-->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- configuration MultipartResolver , the properties of the profile upload -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- The default character encoding -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- The size of the uploaded file , Maximum upload size -->
<property name="maxUploadSize" value="1024000"></property>
</bean>
</beans>
handler class methods: Methods to upload and download files
@Controller
public class SpringMVCTest {
@Autowired
private EmployeeDao employeeDao;
// Implement the file download
// It should be noted that no other configuration is required for uploading and downloading files
@RequestMapping("testResponseEntity")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{
byte[] body=null;
ServletContext servletContext=session.getServletContext();
///files/abc.txt : The address of the file to be downloaded
InputStream in=servletContext.getResourceAsStream("/files/abc.txt");
body=new byte[in.available()];
in.read(body);
HttpHeaders headers=new HttpHeaders();
// The name and value of the response header
headers.add("Content-Disposition", "attachment;filename=abc.txt");
HttpStatus statusCode=HttpStatus.OK;
ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body, headers, statusCode);
return response;
}
// File upload,
@RequestMapping("/testFileUpload")
public String testFileUpload(@RequestParam("desc") String desc,
@RequestParam("file") MultipartFile file) throws IOException{
System.out.println("desc:"+desc);
System.out.println("OriginalFilename"+file.getOriginalFilename());
System.out.println("InputStream"+file.getInputStream());
return "success";
}
}
jsp page: ES26en. jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<!-- File upload form -->
<form action="testFileUpload" method="post" enctype="multipart/form-data">
File:<input type="file" name="file"/>
Desc:<input type="text" name="desc"/>
<input type="submit" value="Submit"/>
</form>
<br><br>
<!-- File download -->
<a href="testResponseEntity" rel="external nofollow" >Test ResponseEntity</a>
</center>
</body>
</html>
success. jsp page: shows that the file was uploaded successfully
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Success page</h3>
</body>
</html>