springBoot上传限制
前言
Github:https://github.com/HealerJean
1、上传大小限制
maxFileSize
是单个文件大小
maxRequestSize
是设置总上传的数据大小
1.1、SpringBoot
限制
Spring Boot 1.3
或之前的版本,配置:
multipart.maxFileSize = 100Mb
multipart.maxRequestSize=150Mb
Spring Boot 1.4
版本后配置更改为:
spring.http.multipart.maxFileSize = 100Mb
spring.http.multipart.maxRequestSize = 150Mb
Spring Boot 2.0
之后的版本配置修改为: 单位Mb改为MB了
spring.servlet.multipart.max-file-size = 100MB
spring.servlet.multipart.max-request-size = 150MB
1.2、前端也可以自己控制
1.3、Nginx
控制的上传大小大小
上传文件的大小超出了 Nginx
允许的最大值,如果没有配置的话,默认是1M
;
client_max_body_size 100m; 上传限制为 100m
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 100m; 上传限制为 100m
2、上传类型限制
@ApiOperation(
value = "文件上传",
notes = "文件上传",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
response = String.class)
@PostMapping(value = "upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String upload(MultipartFile file) {
log.info("文件管理--------文件上传--------请求参数{}", file);
try {
boolean flag = FileUploadContentTypeFilterUtils.checkContentType(file.getInputStream());
if (!flag) {
throw new ParameterErrorException("该文件类型不允许上传");
}
} catch (IOException e) {
e.printStackTrace();
}
//1、确定文件存储目录
String javaIoTmpdir = System.getProperty("java.io.tmpdir");
File tempFile = new File("D:");
if (!tempFile.exists()) {
tempFile.mkdirs();
}
// 2、文件上传
String fileName = file.getOriginalFilename();
File outFile = new File(tempFile, fileName);
try {
// 1、inputstream -> 本地文件
FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
log.info("文件管理--------文件上传成功--------上传文件名{}", file.getOriginalFilename());
} catch (IOException e) {
log.info("文件上传失败");
throw new RuntimeException("文件上传失败", e);
} finally {
log.info("准备开始关闭流");
}
return fileName;
}
2.1、工具类
package com.healerjean.proj.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @author HealerJean
* @ClassName FileTypeRestrictionUtils
* @date 2020/9/4 16:11.
* @Description 当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,
* 再根据MIME Type设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件。
*/
public class FileUploadContentTypeFilterUtils {
//默认判断文件头前三个字节内容
private static int CHECK_BYTES_NUMBER = 3;
private static final Map<String, String> FILE_TYPE_MAP = new HashMap<>();
private FileUploadContentTypeFilterUtils() {
}
static {
FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg");
FILE_TYPE_MAP.put("89504e470d0a1a0a0000", "png");
FILE_TYPE_MAP.put("47494638396126026f01", "gif");
FILE_TYPE_MAP.put("49492a00227105008037", "tif");
FILE_TYPE_MAP.put("424d228c010000000000", "bmp");
FILE_TYPE_MAP.put("424d8240090000000000", "bmp");
FILE_TYPE_MAP.put("424d8e1b030000000000", "bmp");
FILE_TYPE_MAP.put("41433130313500000000", "dwg");
FILE_TYPE_MAP.put("3c21444f435459504520", "html");
FILE_TYPE_MAP.put("3c21646f637479706520", "htm");
FILE_TYPE_MAP.put("48544d4c207b0d0a0942", "css");
FILE_TYPE_MAP.put("696b2e71623d696b2e71", "js");
FILE_TYPE_MAP.put("7b5c727466315c616e73", "rtf");
FILE_TYPE_MAP.put("38425053000100000000", "psd");
FILE_TYPE_MAP.put("46726f6d3a203d3f6762", "eml");
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc");
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "vsd");
FILE_TYPE_MAP.put("5374616E64617264204A", "mdb");
FILE_TYPE_MAP.put("252150532D41646F6265", "ps");
FILE_TYPE_MAP.put("255044462d312e350d0a", "pdf");
FILE_TYPE_MAP.put("2e524d46000000120001", "rmvb");
FILE_TYPE_MAP.put("464c5601050000000900", "flv");
FILE_TYPE_MAP.put("00000020667479706d70", "mp4");
FILE_TYPE_MAP.put("49443303000000002176", "mp3");
FILE_TYPE_MAP.put("000001ba210001000180", "mpg");
FILE_TYPE_MAP.put("3026b2758e66cf11a6d9", "wmv");
FILE_TYPE_MAP.put("52494646e27807005741", "wav");
FILE_TYPE_MAP.put("52494646d07d60074156", "avi");
FILE_TYPE_MAP.put("4d546864000000060001", "mid");
FILE_TYPE_MAP.put("504b0304140000000800", "zip");
FILE_TYPE_MAP.put("526172211a0700cf9073", "rar");
FILE_TYPE_MAP.put("235468697320636f6e66", "ini");
FILE_TYPE_MAP.put("504b03040a0000000000", "jar");
FILE_TYPE_MAP.put("4d5a9000030000000400", "exe");
FILE_TYPE_MAP.put("3c25402070616765206c", "jsp");
FILE_TYPE_MAP.put("4d616e69666573742d56", "mf");
FILE_TYPE_MAP.put("3c3f786d6c2076657273", "xml");
FILE_TYPE_MAP.put("494e5345525420494e54", "sql");
FILE_TYPE_MAP.put("7061636b616765207765", "java");
FILE_TYPE_MAP.put("406563686f206f66660d", "bat");
FILE_TYPE_MAP.put("1f8b0800000000000000", "gz");
FILE_TYPE_MAP.put("6c6f67346a2e726f6f74", "propert");
FILE_TYPE_MAP.put("cafebabe0000002e0041", "class");
FILE_TYPE_MAP.put("49545346030000006000", "chm");
FILE_TYPE_MAP.put("04000000010000001300", "mxp");
FILE_TYPE_MAP.put("504b0304140006000800", "docx");
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");
FILE_TYPE_MAP.put("6431303a637265617465", "torrent");
FILE_TYPE_MAP.put("6D6F6F76", "mov");
FILE_TYPE_MAP.put("FF575043", "wpd");
FILE_TYPE_MAP.put("CFAD12FEC5FD746F", "dbx");
FILE_TYPE_MAP.put("2142444E", "pst");
FILE_TYPE_MAP.put("AC9EBD8F", "qdf");
FILE_TYPE_MAP.put("E3828596", "pwl");
FILE_TYPE_MAP.put("2E7261FD", "ram");
}
/**
* 校验是否在枚举中存在
*/
public static boolean checkContentType(InputStream in) {
String fileType = getFileType(in);
if (fileType != null) {
return true;
}
return false;
}
public static String getFileType(InputStream in) {
String res = null;
try {
byte[] b = new byte[CHECK_BYTES_NUMBER];
in.read(b, 0, b.length);
String fileCode = bytesToHexString(b);
//这种方法在字典的头代码不够位数的时候可以用但是速度相对慢一点
Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();
while (keyIter.hasNext()) {
String key = keyIter.next();
if (key.toLowerCase().startsWith(fileCode.toLowerCase()) || fileCode.toLowerCase().startsWith(key.toLowerCase())) {
res = FILE_TYPE_MAP.get(key);
break;
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
return res;
}
/**
* 得到上传文件的文件头
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
}