您的位置:首页 > 编程语言 > Java开发

kendo ui upload html + java + springmvc

2015-10-19 11:04 483 查看
kendo ui相关资料网上一直不好找,这里关于kendoUI上传文件记录一下,html + java + springmvc

页面:

<div class="demo-section k-content">
<input name="files" id="files" type="file" />
</div>
JS:这里需要注意下,后台传递过来的数据kendoUI全部封装到了response(此response非彼response)里

<script>
$(document).ready(function () {
$("#files").kendoUpload({
multiple: true,
async: {
saveUrl: "mcinfo/uploadDoc",
removeUrl: "mcinfo/removeDoc",
autoUpload: true
},
files: initialFiles,
success: onSuccess//上传成功回调,还有很多时间,不一一介绍
});

function onSuccess(e) {
alert(JSON.stringify(e.response));//后台传递过来的数据全部封装在response中
// 这里根据自己的需要做处理
}
});
</script>


后台:

/* 这个是用来请求的地址 */
@RequestMapping(value = "/uploadDoc")
@ResponseBody
public Map<String, Object> uploadDoc(@RequestParam List<MultipartFile> files, @RequestParam String folder) throws FileNotFoundException, IOException{
Map<String, Object> result = new HashMap<String, Object>();
String fileName = UUID.randomUUID() + ".jpg";
if(StrUtil.isNull(folder)){
folder = UUID.randomUUID().toString();
}
String uploadpath="E:/mcdoc/" + folder;
for(MultipartFile file : files) {
UploadFileUtil.fileUpload(file.getInputStream(), fileName, uploadpath);
}
result.put("uploadpath", uploadpath);
result.put("folder", folder);
result.put("success", true);
return result;
}
这个是上传方法:

/**
* 文件上传
* @param file
* @param filename
* @param upload_path
* @throws FileNotFoundException
* @throws IOException
*/
public static void fileUpload(InputStream in, String filename, String upload_path)
throws FileNotFoundException, IOException {
File uploadFolder = new File(upload_path);
if (!uploadFolder.exists()) {
uploadFolder.mkdir();
}
File uploadFile = new File(uploadFolder + "/" + filename);
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
到此完毕,kendoUI上传就是这么简单
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息