一.文件上传:
这里只使用不用导包的part方法
1:上传单个文件
@PostMapping("uploadByPart2")
public String uploadByPart2(HttpServletRequest request) throws IOException, ServletException {
//找到要存放文件的绝对路径
String realPath = request.getServletContext().getRealPath("/uploadExer/");
//看文件夹是否存在
File file = new File(realPath);
while(!file.exists()){
file.mkdirs();
}
//创建名称,防止重名
Part images = request.getPart("image");
String extension = StringUtils.getFilenameExtension(images.getSubmittedFileName());
long nanoTime = System.nanoTime();
images.write(realPath+nanoTime+"."+extension);
//此处的success是我自己创建的success.jsp页面
return "success";
}
2:上传多个文件
//用Part方式处理多文件上传
@PostMapping("uploadByparts")
public String uploadByparts(HttpServletRequest request) throws IOException, ServletException {
Collection<Part> parts = request.getParts();
String realPath = request.getServletContext().getRealPath("/uploadFiles/");
File file = new File(realPath);
while(!file.exists()){
file.mkdirs();
}
parts.forEach(s->{
String filenameExtension = StringUtils.getFilenameExtension(s.getSubmittedFileName());
long nanoTime = System.nanoTime();
try {
s.write(realPath+nanoTime+"."+filenameExtension);
} catch (IOException e) {
e.printStackTrace();
}
});
return "success";
}
二:下载
@GetMapping("download")
public ResponseEntity<byte[]> fun1(HttpServletRequest request) throws IOException {
String realPath = request.getServletContext().getRealPath("/download/第一课.docx");
FileInputStream in = new FileInputStream(realPath);
byte[] buffer = new byte[in.available()];
in.read(buffer);
//调起浏览器下载功能(通过响应头,告知浏览器这个请求是一个下载)
HttpHeaders httpHeaders = new HttpHeaders();
//设置下载功能
httpHeaders.setContentDispositionFormData("attachment","第一课.docx");
ResponseEntity<byte[]> responseEntity=new ResponseEntity<>(buffer,httpHeaders, HttpStatus.OK);
return responseEntity;
}
当然了,其实web.xml配置以及springmvc.xml配置也很重要,不可缺少
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<multipart-config/>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.javasm.springmvc.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>-->
</beans>

博客主要介绍了SpringMVC中的文件上传和下载功能。文件上传使用无需导包的part方法,包括单个文件和多个文件上传。同时强调了web.xml和springmvc.xml配置的重要性,这些配置在文件上传和下载过程中不可或缺。
4万+

被折叠的 条评论
为什么被折叠?



