layui 导入文件(excel)

jsp代码
 

js代码

layui.use(['element','upload'], function() {
	element = layui.element;
	upload = layui.upload;

	//指定允许上传的文件类型
	upload.render({
	  elem: '#uploadExcel'
	  ,url: path+'/FacjudgController/uploadImg.do'
	  ,accept: 'file' //普通文件
      	  ,multiple: true
	  ,done: function(res){
	      console.log(res);
	   }
	});
});

java代码

@RequestMapping("/uploadImg")
@ResponseBody
public Map uploadImg(@RequestParam MultipartFile file) 
HttpServletRequest request, HttpServletResponse response) throws Exception 
{
		// 判断文件名是否为空
		if (file == null)
			return null;
		Map map = new HashMap();
		// 获取文件名
		String name = file.getOriginalFilename();

		// 判断文件大小、即名称
		long size = file.getSize();
		if (name == null || ("").equals(name) && size == 0)
			return null;

		try 
		{
			// 把文件转换成字节流形式
			int i = facjudgService.batchImport(name, file);
			if (i > 0) 
			{
				String Msg = "批量导入EXCEL成功!";
				map.put("msg", Msg);
				map.put("success", true);
			}
			else 
			{
				String Msg = "批量导入EXCEL失败!";
				map.put("success", false);
				map.put("message", Msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
}

你可能感兴趣的:(layui,java,js)