页面上传多文件
var formdata = new FormData(document.getElementById("proofForm"));
$.ajax({
type: "POST",
url: "#springUrl('/')proofofbill/proof",
data:formdata,
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false, // 告诉jQuery不要去设置Content-Type请求头
success: function(data){
if('1'==data.code){
alert("举证成功");
}else{
//coding
}
},
error: function (data, status, e){
alert("网络错误")
}
});
参考了很多资料,发现不能直接转为file,要在本地写临时文件。
//方法一
File file = new File(DEST_DIR + File.separator,multipartFile.getOriginalFilename());
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
//方法二
File file = multipartToFile(multipartFile);
//方法三
File file = convert(multipartFile);
public File multipartToFile(MultipartFile multipart) throws IllegalStateException, IOException
{
File convFile = new File( multipart.getOriginalFilename());
multipart.transferTo(convFile);
return convFile;
}
public File convert(MultipartFile file) throws IOException {
File convFile = new File(file.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}