本文主要是介绍【第16章】spring-mvc之多文件上传,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 前言
- 一、文件大小限制
- 二、前端
- 三、后端
- 总结
前言
本章在上篇文件的上传基础上就行扩展,多文件上传、多线程处理,所有的文件上传成功则返回成功。
一、文件大小限制
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"version="6.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:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup><multipart-config><max-file-size>10485760</max-file-size><max-request-size>52428800</max-request-size></multipart-config></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
二、前端
<%--Created by IntelliJ IDEA.User: 张军国001Date: 2024/5/3Time: 10:32To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>文件上传和下载</title>
</head>
<body>
<h1><%= "多文件上传演示" %></h1>
<br/>
<form action="${pageContext.request.contextPath}/file/multiple" method="post" enctype="multipart/form-data"><span>文件:</span><input type="file" name="files" multiple><button type="submit">多文件上传</button>
</form>
</body>
</html>
三、后端
后端代码对文件上传个数进行了限制,开启线程池提升多文件处理效率。
package org.example.springmvc.params.controller;import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** Create by zjg on 2024/5/3*/
@RequestMapping("/file/")
@RestController
public class FileController {String path="WEB-INF/nas";Integer maxFileNumber=5;ExecutorService executorService = Executors.newFixedThreadPool(maxFileNumber);@RequestMapping("multiple")public void multiple(@RequestPart MultipartFile[] files, HttpServletRequest request,HttpServletResponse response) throws IOException, ExecutionException, InterruptedException {if(files.length>maxFileNumber){// 如果文件不存在,则返回400错误response.sendError(HttpServletResponse.SC_BAD_REQUEST);System.out.println(String.format("多文件上传超过最大数量[%s]",maxFileNumber));return;}CompletableFuture[] completableFutures=new CompletableFuture[maxFileNumber];for(int i=0;i<files.length;i++){MultipartFile file=files[i];completableFutures[i]= CompletableFuture.runAsync(()->{String filename = file.getOriginalFilename();String realPath = request.getServletContext().getRealPath(path);File dir = new File(realPath);if(!dir.exists()){dir.mkdirs();}try {file.transferTo(new File(realPath+File.separator+filename));} catch (IOException e) {throw new RuntimeException(e);}System.out.println(String.format("[%s]上传成功",filename));},executorService);}CompletableFuture.allOf(completableFutures).get();System.out.println("响应");response.setStatus(200);}
}
总结
回到顶部
这篇关于【第16章】spring-mvc之多文件上传的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!