文件上传-pubsec-文件上传大小限制
Caused by: java.lang.IllegalArgumentException: ContextPath must start with '/' and not end with '/'
错误代码:
server:
servlet:
context-path: mozq
原因:不能以斜杠开头,也不能以斜杠结尾。
# 以斜杠开头的路径表示绝对路径和域名端口号直接拼接。
<form method="post" action="/file/upload" enctype="multipart/form-data">
<input type="file" name="userImg">
<input type="submit" value="提交">
</form>
实际请求的路径:http://localhost:8080/file/upload 报错。
项目contextPath=/mozq
代码:
@RestController
@RequestMapping("/file")
public class FileController {
@RequestMapping("/upload")
public Map<String, Object> upload(MultipartFile file){
return null;
}
}
方案:
action="file/upload" 不以斜杠开头。使用相对路径。
org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (62488249) exceeds the configured maximum (10485760)
原因:上传文件大小超过限制。
@Configuration
public class MultiPartConfig {
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 单个数据大小
factory.setMaxFileSize(DataSize.of(100, DataUnit.MEGABYTES));
/// 总上传数据大小
factory.setMaxRequestSize(DataSize.of(300, DataUnit.MEGABYTES));
return factory.createMultipartConfig();
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery.js"></script>
<style>
.hide{
display: none;
}
.tip{
color: red;
font-size: 12px;
}
</style>
</head>
<body>
<form id="submitForm">
秘钥文件:<input id="secFile" name="secFile" type="file" onclick="hide('secFile')"><br/>
<div id="secFileCheck" class="hide tip">请选择密钥文件</div>
公钥文件:<input id="pubFile" name="pubFile" type="file" onclick="hide('pubFile')"><br/>
<div id="pubFileCheck" class="hide tip" >请选择公钥文件</div>
密码:<input name="password" type="text" onblur="checkPassword()" onclick="hide('password')"><br/>
<div id="passwordCheck" class="hide tip">请输入密码</div>
<input value="提交" type="button" onclick="submitForm()" ><br/>
</form>
<script>
function submitForm() {
var pubFile = document.getElementById("pubFile").files[0];
var secFile = document.getElementById("secFile").files[0];
var password = $("input[name='password']").val();
console.log("私钥文件:");
console.log(secFile);
console.log("公钥文件:");
console.log(pubFile);
console.log("私钥文件密码:" + password);
if(checkFile("pubFile") & checkFile("secFile") & checkPassword()){
var formData = new FormData();
formData.append("pubFile", pubFile);
formData.append("secFile", secFile);
formData.append("password", password);
$.ajax({
url: "isPubAndSecretMatch",
type: "post",
data: formData,
contentType: false,
processData: false,
mimeType: "multipart/form-data",
success: function (data) {
console.log("响应数据:" + data);
data = JSON.parse(data);
if(data.flag == "success"){
alert("公钥和私钥匹配成功");
}else if(data.flag == "fail"){
alert(data.message);
}
}
})
}
}
function hide(ele) {
$("#" + ele + "Check").addClass("hide");
}
function checkFile(fileEleId) {
var fileEle = document.getElementById(fileEleId).files[0];
if(fileEle == null){
$("#" + fileEleId + "Check").removeClass("hide");
return false
}else{
return true;
}
}
function checkPassword() {
var password = $("input[name='password']").val();
if(password == null || password==''){
$("#passwordCheck").removeClass("hide");
return false;
}else{
return true;
}
}
</script>
</body>
</html>