【发布时间】:2015-12-17 05:05:05
【问题描述】:
在我当前的 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);
}
并通过服务类中的这个方法:
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
但是当我尝试上传文件时,我收到了上述错误(在标题中)。谁能告诉我这里出了什么问题?
【问题讨论】:
-
你能添加你的控制器 RequestMapping 来上传吗?
标签: spring file-upload spring-boot multipartform-data multipart