无法解析多部分 servlet 请求;嵌套异常是 FileUploadException:请求被拒绝,因为没有找到多部分边界
Could not parse multipart servlet request; nested exception is FileUploadException: the request was rejected because no multipart boundary was found
在我当前的 spring-boot 项目中,我使用此表单将 war 文件上传到服务器:
<form:war>
<div class="field-box">
<label th:text="${war}"></label>
<div class="col-md-7">
<input class="form-control arquivo" th:attr="data-classe=${command['class'].simpleName},target=${war}" th:id="${war}" type="file" />
</div>
</div>
</form:war>
表单由此控制器方法处理:
@RequestMapping(value="download", method=RequestMethod.GET)
@ResponseBody
public HttpEntity<byte[]> download(@RequestParam(value="id") String id) throws Exception {
return this.serv.download(id);
}
并在服务中使用此方法 class:
public String upload(String id, String classe, String target, MultipartFile file) throws Exception {
Arquivo novo;
if(id == null) {
novo = new Arquivo(classe, target);
this.dao.insert(novo);
id = novo.getId();
} else {
novo = this.dao.findById(id);
}
byte[] bytes = file.getBytes();
File arquivo = new File(novo.toString());
if(!arquivo.exists())
if(arquivo.mkdirs())
arquivo.createNewFile();
BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(arquivo) );
stream.write(bytes);
stream.close();
return novo.getId();
}
这一切都是由这个jquery代码触发的:
$("input.arquivo").on("change", function(event){
console.log('mudanca no campo de arquivo');
var c = $(this).data('classe');
var t = $(this).data('target');
var f = $(this).val();
$.ajax({
method: "POST",
url: "/Picture/upload",
data: { classe: c, target: t, file: f },
contentType: "multipart/form-data"
}).done(function(data){
console.log('id: '+data);
$(this).attr('type', 'hidden');
$(this).attr('name', t);
$(this).attr('value', data)
});
});
我的application.properties有这样的配置:
# Multipart Configuration
multipart.location = ${user.home}/.lojacms/Uploads
multipart.maxFileSize = 100Mb
multipart.maxRequestSize = 100Mb
multipart.fileSizeThreshold = 10Mb
但是当我尝试上传文件时,出现上述错误(在标题中)。谁能告诉我这里出了什么问题?
好的,然后我找到了将 javascript 代码更改为这样的解决方案:
$("input.arquivo").on("change", function(event){
var c = $(this).data('classe');
var t = $(this).data('target');
var formData = new FormData();
formData.append('classe', c);
formData.append('target', t);
formData.append('file', $('#input_'+t)[0].files[0]);
$.ajax({
method: "POST",
url: "/Arquivo/upload",
data: formData,
cache: false,
contentType: false,
processData: false
}).done(function(data){
$('#'+t).append('<input type="hidden" name="'+t+'" value="'+data+'"/>')
});
});
简而言之,我正在使用 javascript 的 FormData
元素来处理提交给服务器的值,包括多部分数据。
在我当前的 spring-boot 项目中,我使用此表单将 war 文件上传到服务器:
<form:war>
<div class="field-box">
<label th:text="${war}"></label>
<div class="col-md-7">
<input class="form-control arquivo" th:attr="data-classe=${command['class'].simpleName},target=${war}" th:id="${war}" type="file" />
</div>
</div>
</form:war>
表单由此控制器方法处理:
@RequestMapping(value="download", method=RequestMethod.GET)
@ResponseBody
public HttpEntity<byte[]> download(@RequestParam(value="id") String id) throws Exception {
return this.serv.download(id);
}
并在服务中使用此方法 class:
public String upload(String id, String classe, String target, MultipartFile file) throws Exception {
Arquivo novo;
if(id == null) {
novo = new Arquivo(classe, target);
this.dao.insert(novo);
id = novo.getId();
} else {
novo = this.dao.findById(id);
}
byte[] bytes = file.getBytes();
File arquivo = new File(novo.toString());
if(!arquivo.exists())
if(arquivo.mkdirs())
arquivo.createNewFile();
BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(arquivo) );
stream.write(bytes);
stream.close();
return novo.getId();
}
这一切都是由这个jquery代码触发的:
$("input.arquivo").on("change", function(event){
console.log('mudanca no campo de arquivo');
var c = $(this).data('classe');
var t = $(this).data('target');
var f = $(this).val();
$.ajax({
method: "POST",
url: "/Picture/upload",
data: { classe: c, target: t, file: f },
contentType: "multipart/form-data"
}).done(function(data){
console.log('id: '+data);
$(this).attr('type', 'hidden');
$(this).attr('name', t);
$(this).attr('value', data)
});
});
我的application.properties有这样的配置:
# Multipart Configuration
multipart.location = ${user.home}/.lojacms/Uploads
multipart.maxFileSize = 100Mb
multipart.maxRequestSize = 100Mb
multipart.fileSizeThreshold = 10Mb
但是当我尝试上传文件时,出现上述错误(在标题中)。谁能告诉我这里出了什么问题?
好的,然后我找到了将 javascript 代码更改为这样的解决方案:
$("input.arquivo").on("change", function(event){
var c = $(this).data('classe');
var t = $(this).data('target');
var formData = new FormData();
formData.append('classe', c);
formData.append('target', t);
formData.append('file', $('#input_'+t)[0].files[0]);
$.ajax({
method: "POST",
url: "/Arquivo/upload",
data: formData,
cache: false,
contentType: false,
processData: false
}).done(function(data){
$('#'+t).append('<input type="hidden" name="'+t+'" value="'+data+'"/>')
});
});
简而言之,我正在使用 javascript 的 FormData
元素来处理提交给服务器的值,包括多部分数据。